SQL Server: Populate dates without table

I needed a list of continuous dates between a specified range similar to generating serial numbers.
DECLARE @startDate DATETIME
DECLARE @endDate DATETIME

SET @startDate = '2016-01-01'
SET @endDate = '2016-01-31';

WITH populateAllDates(Date) AS
(
SELECT @startdate as Date UNION ALL SELECT DATEADD(d,1,[Date])
FROM populateAllDates
WHERE DATE < @enddate
)
SELECT Date FROM populateAllDates OPTION (MAXRECURSION 0)

RESULTS

No comments:

Post a Comment