Query Help (SQL Calander)

  • Hi All,

    I need one Query

    based on fromdate and todate i want to get output like this

    example:

    declare @fromdate ='7-10-2012 ',@todate='20-10-2012'

    sunday monday tuesday wednesday thursday friday saturday

    7-10-2012 8-10-2012 9-10-2012 10-10-2012 11-10-2012 12-10-2012 13-10-2012

    14-10-2012 15-10-2012 16-10-2012 17-10-2012 18-10-2012 19-10-2012 20-10-2012

  • Hi,

    Try:

    declare @fromdate date = '2012-10-7', @todate date = '2012-10-20';

    with CTE as

    (

    select

    @fromdate as Sunday,

    DATEADD(day, 1, @fromdate) as Monday,

    DATEADD(day, 2, @fromdate) as Tuesday,

    DATEADD(day, 3, @fromdate) as Wednesday,

    DATEADD(day, 4, @fromdate) as Thursday,

    DATEADD(day, 5, @fromdate) as Friday,

    DATEADD(day, 6, @fromdate) as Saturday

    union all

    select

    DATEADD(day, 7, Sunday),

    DATEADD(day, 7, Monday),

    DATEADD(day, 7, Tuesday),

    DATEADD(day, 7, Wednesday),

    DATEADD(day, 7, Thursday),

    DATEADD(day, 7, Friday),

    DATEADD(day, 7, Saturday)

    from CTE

    where Saturday < @todate

    )

    select * from CTE

    Hope it's useful.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply