• AlanLSmith (6/11/2013)


    Suggest that master dates table is best.

    But if it's not available, here's a query that will give you the dates for a given year and month.

    Still working on how to get the cross-tab/pivot/matrix working to have these in columns, but this is a start:

    with daylist as

    (

    select 1 as daynum

    union all

    select dl.daynum + 1 as daynum

    from daylist dl

    where dl.daynum + 1 <=

    day( DATEADD( s, -1, DATEADD( mm, DATEDIFF( m, 0,

    CAST(@yearnum + '-' + @monthnum + '-01' AS datetime))+1,0)))

    )

    select daynum from daylist order by daynum;

    Alan,

    Please see the article at the following link. You might change your mind about using rCTE's (even small ones) that count in the future.

    http://www.sqlservercentral.com/articles/T-SQL/74118/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)