Wednesday, February 25, 2009

How To Get The Last Day of The Month - PL SQL

Having problems with PL SQL when trying to get the last day of a month? Well, I have good news for you then. Let me give an example of what I try to explain...

Assume that you have a date variable (or data whatever you want to call it) and you want to get the last day of the same month. If we have 2009-02-03 (YYYY-MM-DD format is used), you should get 2009-02-28 as output, hence February is 28 days in 2009.

Below is the solution in query form:

SELECT TRUNC(LAST_DAY(DATE'2009-02-03')) AS LD
FROM DUAL

And here it comes with '2009-02-28' as output. Happy PL SQL writing...