Saturday 13 July 2013

GET CALENDER FROM 1-JAN-0001 TO 31-DEC-9999:

GET CALENDER FROM 1-JAN-0001 TO 31-DEC-9999:
==================================
THE FOLLOWING SQL CODE GIVES CALENDER FOR 1000 YEARS:
==========================================

SELECT MONTH,"SUN","MON","TUE","WED","THU","FRI","SAT" FROM
(
SELECT TO_CHAR(RW,'MONTH YYYY') MONTH,
TO_CHAR(RW+1,'IW') WEEK,
MAX(DECODE(TO_CHAR(RW,'D'),'1',LPAD(TO_CHAR(RW,'FMDD'),2))) "SUN",
MAX(DECODE(TO_CHAR(RW,'D'),'2',LPAD(TO_CHAR(RW,'FMDD'),2))) "MON",
MAX(DECODE(TO_CHAR(RW,'D'),'3',LPAD(TO_CHAR(RW,'FMDD'),2))) "TUE",
MAX(DECODE(TO_CHAR(RW,'D'),'4',LPAD(TO_CHAR(RW,'FMDD'),2))) "WED",
MAX(DECODE(TO_CHAR(RW,'D'),'5',LPAD(TO_CHAR(RW,'FMDD'),2))) "THU",
MAX(DECODE(TO_CHAR(RW,'D'),'6',LPAD(TO_CHAR(RW,'FMDD'),2))) "FRI",
MAX(DECODE(TO_CHAR(RW,'D'),'7',LPAD(TO_CHAR(RW,'FMDD'),2))) "SAT"
FROM
(
SELECT (TO_DATE('1-JAN-0001','DD-MON-YYYY')-1)+LEVEL RW
FROM DUAL
CONNECT BY
LEVEL<=ADD_MONTHS((TO_DATE('1-JAN-0001','DD-MON-YYYY')-1),12*9999)-TO_DATE('1-JAN-0001','DD-MON-YYYY')
)
GROUP BY TO_CHAR(RW,'MONTH YYYY'),TO_CHAR(RW+1,'IW')
ORDER BY MONTH,WEEK
)
ORDER BY TO_DATE(MONTH,'MONTH YYYY'),TO_NUMBER(WEEK)
/

SET PAGESIZE 200
BREAK ON MONTH SKIP 1
COL MONTH FOR A18
COL SUN FOR A5
COL MON FOR A5
COL TUE FOR A5
COL WED FOR A5
COL THU FOR A5
COL FRI FOR A5
COL SAT FOR A5

No comments:

Post a Comment