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 ...

Get Oracle and PL/SQL Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.