• mohit.sharma 99294 (8/13/2013)


    We can achieve same with date part.

    SELECT [Month] = DATENAME(mm,Created_Date), Sale

    FROM

    (

    SELECT Created_Date = '2012-12-01', Sale = 10000

    UNION

    SELECT Created_Date = '2012-02-01', Sale = 1000

    UNION

    SELECT Created_Date = '2012-03-01', Sale = 2500

    UNION

    SELECT Created_Date = '2012-04-01', Sale = 12000

    UNION

    SELECT Created_Date = '2012-05-01', Sale = 5500

    UNION

    SELECT Created_Date = '2012-06-01', Sale = 6500

    UNION

    SELECT Created_Date = '2012-07-01', Sale = 7500

    UNION

    SELECT Created_Date = '2012-08-01', Sale = 20000

    UNION

    SELECT Created_Date = '2012-09-01', Sale = 1500

    UNION

    SELECT Created_Date = '2012-10-01', Sale = 15000

    UNION

    SELECT Created_Date = '2012-11-01', Sale = 4000

    UNION

    SELECT Created_Date = '2012-01-01', Sale = 50000

    ) T1

    ORDER BY DATEPART(MM,Created_Date)

    Yep. For single rows, that will work just fine. Try your code with a GROUP BY and a SUM on the Sale column and see what happens. 😉

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