7-8. Returning the Last Day of a Given Month

Problem

You want to have the ability to obtain the last day for a given month.

Solution

Use the Oracle built-in LAST_DAY function to return the last day of the month for the date that you pass into it. The following example demonstrates a code block in which the LAST_DAY function is used to return the last day of the current month:

DECLARE
  last_day  VARCHAR2(20);
BEGIN
  select LAST_DAY(sysdate)
  INTO last_day
  FROM DUAL;
  DBMS_OUTPUT.PUT_LINE(last_day);
END;

How It Works

The LAST_DAY function is an easy way to retrieve the date for the last day of a given date. To use the function, pass in any date, and the last day of the month for the given date will be returned. The function can be useful in combination with other functions, especially for converting strings into dates and then determining the last day of the given month for the date given in string format. For example, the following combination is used quite often:

LAST_DAY(to_date(string_based_date,'MM/DD/YYYY'))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.222.162