Tuesday, September 9, 2008

Determining Leap Year in Oracle and Days in February

Recently, I was playing around with a leap year issue and the number of days in February. I would like to share the following snippets of commands to help some people save some time.

--2 ways to determine if it is a LEAP YEAR in ORACLE

SELECT 2008 , Decode( mod(2008, 4), 0, decode( mod(2008, 400), 0, 'Yes', decode( mod(2008, 100), 0, 'No', 'Yes') ), 'No' ) as LEAP_YEAR
FROM DUAL;

or

SELECT DECODE(29, (SELECT to_date('01032008', 'DDMMYYYY') - to_date('01022008', 'DDMMYYYY')FROM DUAL),'LEAPYEAR','NOTLEAPYEAR')
FROM DUAL;

--2 ways to determine the NUMBER OF DAYS IN FEBRUARY in ORACLE

SELECT (EXTRACT (DAY FROM LAST_DAY(ADD_MONTHS(TRUNC(TO_DATE('2008','YYYY'),'YYYY'),1))))
FROM DUAL;

or

SELECT to_date('01032008', 'DDMMYYYY') - to_date('01022008', 'DDMMYYYY')
FROM DUAL;



Of course the input to these queries requires the year that you are inquiring about.

4 comments:

Anonymous said...

Bravo, what phrase..., an excellent idea

Anonymous said...

Nice brief and this mail helped me alot in my college assignement. Thank you seeking your information.

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now. Keep it up!
And according to this article, I totally agree with your opinion, but only this time! :)

Anonymous said...

i truly adore all your posting way, very interesting.
don't quit as well as keep penning mainly because it simply just well worth to look through it,
looking forward to view alot more of your own well written articles, regards!