Home » » generate weekdays in sql

generate weekdays in sql

Author : Sandeep Mittal on Friday, December 28, 2012 | 12/28/2012 10:17:00 AM

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

Share this article :

Post a Comment