Sunday, October 26, 2008

SSIS Junkie : T-SQL: Generate a list of dates

 

The following bit of code uses a common table expression (CTE) to generate a contiguous list of dates in SQL Server.

with mycte as

(

select cast('20080101' as datetime) DateValue

union all

select DateValue + 1

from mycte

where DateValue + 1 < '20080731'

)

select convert(char(8),DateValue,112) DateValue

from mycte

OPTION (MAXRECURSION 0)

SSIS Junkie : T-SQL: Generate a list of dates

No comments: