• And then you better make sure you run the code more than once, even on a quiet system...

    DECLARE @Year INT

    SET @Year = 2008

    SET STATISTICS IO ON

    SET STATISTICS TIME ON

    ;WITH cteDates AS

    (

    SELECT DATEADD(yy,@Year-1900,0)+Number AS TheDate

    FROM Master.dbo.spt_Values

    WHERE Type = 'P'

    AND Number <= 366

    )

    SELECT cd.TheDate, DATEPART(qq,TheDate) AS TheQuarter

    FROM cteDates cd

    WHERE YEAR(TheDate) = @Year

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',100)

    SET STATISTICS TIME ON

    SELECT DATEADD(yy,@Year-1900,0)+Number AS TheDate,

    DATEPART(qq,DATEADD(yy,@Year-1900,0)+Number) AS TheQuarter

    FROM Master.dbo.spt_Values

    WHERE Type = 'P'

    AND Number < 366

    AND YEAR(DATEADD(yy,@Year-1900,0)+Number) = @Year

    SET STATISTICS TIME OFF

    SET STATISTICS IO OFF

    --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)