Friday, December 28, 2012

generate weekdays in sql

In this post, I am sharing simple code to generate weekdays using Recursive CTE. 

Common Table Expression (CTE) was introduced in SQL Server 2005 by Microsoft. 

To learn more about CTE, refer 
Common Table Expresssion in SQL Server 

Refer related posts,
;with days as (    
    select 1 as DayId
            , datename(dw, convert(datetime, '19000101')) as DayName
    union all
    select DayId + 1
            , datename(dw, convert(datetime, cast(19000101 + DayId as varchar))) 
    from    days 
    where    DayId<7
)
select * from days

OUTPUT

0 comments:

Post a Comment