Try it. Use the Recursive CTE .
DECLARE @minDateTime AS DATETIME; DECLARE @maxDateTime AS DATETIME; SET @minDateTime = '2014-01-13 02:00:00'; SET @maxDateTime = '2014-12-31 14:00:00'; ; WITH Dates_CTE AS (SELECT @minDateTime AS Dates UNION ALL SELECT Dateadd(hh, 1, Dates) FROM Dates_CTE WHERE Dates < @maxDateTime) SELECT * FROM Dates_CTE OPTION (MAXRECURSION 0)
The Dates_CTE query Dates_CTE has a Common Expression Table , the base record for CTE is output by the first SQL query before UNION ALL . The result of the query gives you the Minimum date .
The second query after UNION ALL is run again to get the results. This process is recursive and will continue until the Dates are less than @maxDateTime .
P ரதீப்
source share