problem in Order By

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • san1014
    New Member
    • Jul 2007
    • 37

    problem in Order By

    Hi
    I Have a table like this

    code=oracle]

    sql> select month from temp;
    month
    -----------
    Apr
    May
    Jul
    Jun
    dec
    Oct
    Feb
    Nov
    Mar
    Sep
    Jan
    Aug

    Now i want the o/p like this
    sql>select month from temp;

    month
    ----------
    Jan
    Feb
    Mar
    Apr
    may
    Jun
    Jul
    Aug
    Sep
    Oct
    Nov
    Dec

    [/code]


    Is there any special function to get the months values in order?
    Last edited by amitpatel66; Nov 26 '07, 12:14 PM. Reason: code tags
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Try this:

    [code=oracle]
    SELECT col1,TO_CHAR(TO _DATE('01-'||col1||'-2007','DD-MON-YYYY'),'MM') col2 FROM
    (SELECT 'OCT' AS col1 FROM dual
    UNION
    SELECT 'FEB' FROM dual
    UNION
    SELECT 'MAR' FROM dual
    UNION
    SELECT 'DEC' FROM dual
    UNION
    SELECT 'JUN' FROM dual
    UNION
    SELECT 'SEP' FROM dual
    UNION
    SELECT 'JUL' FROM dual
    UNION
    SELECT 'AUG' FROM dual
    UNION
    SELECT 'APR' FROM dual
    UNION
    SELECT 'JAN' FROM dual
    UNION
    SELECT 'NOV' FROM dual
    UNION
    SELECT 'MAY' FROM dual)
    ORDER BY col2
    /
    [/code]

    Comment

    • singhh04
      New Member
      • Nov 2007
      • 4

      #3
      Just you need the following sql to excute; you will get the months as per your order :-

      SELECT month FROM temp ORDER BY to_date(month,' MON');

      Comment

      • Dharmaraju
        New Member
        • Sep 2007
        • 13

        #4
        Originally posted by singhh04
        Just you need the following sql to excute; you will get the months as per your order :-

        SELECT month FROM temp ORDER BY to_date(month,' MON');


        select * from (select to_char(hire_da te,mm)aa ,to_char(hire_d ate,month), aa1 from employees ) a order by aa

        Comment

        Working...