God day!I have a working cte that produces the monthdate base on given dates. USE MFR_Merchandise; declare @FromDate as datedeclare @ToDate as dateset @FromDate='11-02-2011'set @ToDate='02-24-2012'; with rcte as( select FilterDate = dateadd(month, datediff(month, 0, @FromDate) + 1, -1) union all select FilterDate = @ToDate union all select FilterDate = dateadd(month, datediff(month, 0, FilterDate) + 2, -1) from rcte where dateadd(month, 1, FilterDate) <= @ToDate)select *from rcteoption (maxrecursion 0)
The above cte produces the result as below11-30-201112-31-201101-31-201202-24-2012
I need to convert the said cte above into a plain temp table but when I increment the date? It seems its not working. Here is my code so far. The result must be the same as with cte.USE MFR_Merchandise; declare @FromDate as datedeclare @ToDate as datedeclare @MonthDates as Table(MonthsFilter date NOT NULL) set @FromDate='11-02-2011'set @ToDate='02-24-2012'while @FromDate < @ToDate begin insert into @MonthDates select MonthsFilter = dateadd(month, datediff(month, 0, @FromDate) + 1, -1) Set @FromDate = (SELECT Min(MonthsFilter) FROM @MonthDates WHERE MonthsFilter > @FromDate) end select * from @MonthDates
Thank you!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008