TSQL below shows how to build a dynamic calendar table. The query, as shown, changes the date from each year, but the following shows how you can fix the calendar start date for a specific year.
select case when oact.fathernum like '112%' then sum(jdt1.debit) - sum(jdt1.credit) end as [Accounts Receivable], jdt1.refdate as [Posting Date] from jdt1 inner join oact on jdt1.account = oact.AcctCode inner join (select FirstDayOfYear =DATEADD(m,datediff(m,0,getdate())-MONTH(getdate())+1,0), FirstDayOfMonth =DATEADD(m,datediff(m,0,getdate()),0)) D inner join master..spt_values v on v.type='P' and v.number between 0 and 500
The selection creates a single record of two rotation dates, called
inner join (select FirstDayOfYear =DATEADD(m,datediff(m,0,getdate())-MONTH(getdate())+1,0), FirstDayOfMonth =DATEADD(m,datediff(m,0,getdate()),0)) D
The two summary dates are the first day **current year** and the first day of the current month (also in the current year). If you need the first day of the year **specific** and the first day of the month (current month), but in the same specific year, you can use the option below (example 2008 - January-01)
select FirstDayOfYear =cast('20080101' as datetime), FirstDayOfMonth =dateadd(m,month(getdate())-1,'20080101')
It uses summary dates and a built-in sequence of numbers to gradually add 1 year each time to the dates of the summary, starting from adding 0 (for the current year).
inner join master..spt_values v on v.type='P' and v.number between 0 and 500 on jdt1.refdate >= DATEADD(year,v.number,D.FirstDayOfYear) and jdt1.refdate < DATEADD(year,v.number,D.FirstDayOfMonth)
Also note that instead
date between A and B
I usually prefer
date >= A and date < B+1
What works, regardless of whether B includes time information. This does not matter for your request, but is good practice for consistency.