How to get the dates between two dates?
I have a variable @MAXDATE
which is storing the maximum date from the table. Now I want to get the all dates between @Maxdate
and GETDATE()
and want to store these date in a cursor.
So far I have done as follows:
;with GetDates As
(
select DATEADD(day,1,@maxDate) as TheDate
UNION ALL
select DATEADD(day,1, TheDate) from GetDates
where TheDate < GETDATE()
)
This is working perfectly but when I am trying to store these values in a cursor
SET @DateCurSor=CURSOR FOR
SELECT TheDate
FROM GetDates
Compilation Error
Incorrect syntax near the keyword 'SET'.
How to solve this.
Thanks in advance
You can use this script to find dates between two dates. Reference taken from this Article:
I listed dates of 2 Weeks later. You can use variable @period OR function datediff(dd, @date_start, @date_end)
Just saying...here is a more simple approach to this: