Oracle LAST_DAY() Function

Oracle LAST_DAY() is a built-in function that returns the last day of the month for a given date.

Oracle LAST_DAY() Syntax

Here is the syntax for the Oracle LAST_DAY() function:

LAST_DAY(date)

Parameters

date

Required.

Return Value

The Oracle LAST_DAY() function returns the last day of the month for a given date.

The return type of the LAST_DAY() function is always DATE, regardless of the data type of the input date parameter.

If either argument is NULL, LAST_DAY() returns NULL.

Oracle LAST_DAY() Examples

Here are some examples that demonstrate the usage of the Oracle LAST_DAY() function.

Basic Usage

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
SELECT
    LAST_DAY(DATE '2023-02-11')
FROM dual;

Output:

LAST_DAY(DATE'2023-02-11')
_____________________________
2023-02-28

Here, we use the ALTER SESSION statement to modify the date display format of the current session.

Last Day of the Current Month

To get the last day of the current month, use the SYSDATE or CURRENT_DATE function:

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
SELECT
    LAST_DAY(SYSDATE),
    LAST_DAY(CURRENT_DATE)
FROM dual;

Output:

LAST_DAY(SYSDATE)    LAST_DAY(CURRENT_DATE)
____________________ _________________________
2023-02-28           2023-02-28

NULL Parameters

If either argument is NULL, LAST_DAY() returns NULL.

SET NULL 'NULL';
SELECT
    LAST_DAY(NULL)
FROM dual;

Output:

LAST_DAY(NULL)
_________________
NULL

Here, we use the SET NULL 'NULL'; statement to display NULL values as the string 'NULL'.

Conclusion

Oracle LAST_DAY() is a built-in function that returns the last day of the month for a given date.