SQL DECODE to Order Months

Your rating: None Average: 3.8 (4 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!