SQL DECODE to Order Months

Your rating: None Average: 4.5 (2 votes)

The SQL below will order months from Jan to Dec (01 to 12).

SELECT short_month AS short,
  long_month AS long
FROM month_tbl
ORDER BY decode(UPPER(short_month),
  'JAN',   '01',  
  'FEB',   '02',  
  'MAR',   '03',  
  'APR',   '04',  
  'MAY',   '05',  
  'JUN',   '06',  
  'JUL',   '07',  
  'AUG',   '08',  
  'SEP',   '09',  
  'OCT',   '10',  
  'NOV',   '11',  
  'DEC',   '12');

short       long
JAN     JANUARY
FEB     FEBRUARY
MAR     MARCH
APR     APRIL
MAY     MAY
JUN     JUNE
JUL     JULY
AUG     AUGUST
SEP     SEPTEMBER
OCT     OCTOBER
NOV     NOVEMBER
DEC     DECEMBER

Notice how the DECODE function was used to give each months its numeric value.

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!

Comments

Have a question? Please ask it on the forum instead.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
The question below is to prevent automated spam submissions.
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.