Pages

Thursday, September 22, 2011

Dispaly List of Month By SQL

SELECT     TO_CHAR (start_date + LEVEL - 1, 'MM') month_code,
           TO_CHAR (start_date + LEVEL - 1, 'MON') mon_name
      FROM (SELECT TO_DATE (TO_CHAR (SYSDATE, 'YYYY') || '0101',
                            'YYYYMMDD'
                           ) start_date,
                   TO_DATE (TO_CHAR (SYSDATE, 'YYYY') || '1231',
                            'YYYYMMDD'
                           ) end_date
              FROM DUAL)
CONNECT BY start_date + LEVEL - 1 <= end_date
  GROUP BY TO_CHAR (start_date + LEVEL - 1, 'MM'),
           TO_CHAR (start_date + LEVEL - 1, 'MON');

/*
OUTPUT:
MONTH_CODE|MONTH_NAME
01|JAN
02|FEB
03|MAR
04|APR
05|MAY
06|JUN
07|JUL
08|AUG
09|SEP
10|OCT
11|NOV
12|DEC

*/

No comments:

Post a Comment