I want to get only the start date of month between selected date
ex :
start date = 2012 -05-01 end date = 2013-04-01
I want like this
Dates : 2012 -05-01 2012 -06-01 2012 -07-01 ..... 2013 -01-01 ..... 2012 -05-01
I want to get only the start date of month between selected date
ex :
start date = 2012 -05-01 end date = 2013-04-01
I want like this
Dates : 2012 -05-01 2012 -06-01 2012 -07-01 ..... 2013 -01-01 ..... 2012 -05-01
CTE will do the trick
Declare @beginDt SmallDatetime = '13 Jan 2012'
Declare @endDt SmallDatetime = '2 Apr 2013';
With MonthList(aMon)
As
(Select DateAdd(month, datediff(month, 0, @beginDt), 31)
Union All
Select DateAdd(month, 1, aMon) From MonthList
Where DateAdd(month, 1, aMon) < @endDt)
Select aMon from MonthList
First, create a calendar table. Then you can write a simple query:
select [Date]
from dbo.Calendar
where IsFirstDayOfMonth = 1 and [Date] between '20120501' and '20130401'
This query is much clearer than using using a function-based solution and a calendar table has many other uses too.