How to Get the Last Day of the Month in SQL

Below are examples of using SQL to return the last day of the month across various DBMSs.

This could be the last day of the current month, or the last day of a month based on a given date.

SQL Server

In SQL Server’s case, there’s the EOMONTH() function:

SELECT EOMONTH( '2025-01-01' );

Result:

2025-01-31

The EOMONTH() function accepts an optional second argument that can be used to add or subtract one or more months from the date. Therefore, you can use this function to easily find out the last day of next month, last month, etc.

See How to Find the Last Day of the Month in SQL Server for more.

Oracle

Oracle has a LAST_DAY() function:

SELECT LAST_DAY(date '2035-02-15')
FROM DUAL;

Result:

28-FEB-35

The last day of the month is defined by the session parameter NLS_CALENDAR. See How to Check the Values of the NLS Parameters to find out which calendar your session is using. Mine is using the Gregorian calendar.

Unlike SQL Server’s EOMONTH() function, Oracle’s LAST_DAY() function doesn’t accept a second argument that allows us to add or subtract a month. But we can use the ADD_MONTHS() function to do just that, then pass that function to LAST_DAY().

See How to Get the Last Day of the Month in Oracle for more.

MySQL

MySQL has a LAST_DAY() function that returns the last day of a given month:

SELECT LAST_DAY('2030-04-15');

Result:

2030-04-30

MariaDB

Similar to MySQL, MariaDB also has a LAST_DAY() function:

SELECT LAST_DAY('2030-07-15');

Result:

2030-07-31

PostgreSQL

PostgreSQL doesn’t have a LAST_DAY() or EOMONTH() function (or anything similar), so we have to do a bit of extra work:

SELECT (date_trunc('month', date '2030-07-14') + interval '1 month - 1 day')::date;

Result:

2030-07-31

The DATE_TRUNC() function is quite handy though, as it truncates the date to a certain precision. In our case, we truncated it to the month, meaning that it returned the first day of the month. From there, it was a simple matter of adding a month (to get the start of next month), then subtracting a day from that to bring it back to the end of this month.

SQLite

In SQLite, we can use the DATE() function:

SELECT DATE('2030-10-17', 'start of month', '+1 month', '-1 day');

Result:

2030-10-31

The process is similar to how we did it with PostgreSQL (although the functions are different). The 'start of month' argument modifies the date to the beginning of the month. We then use '+1 month' to add a month to the month’s beginning (which would shift it forward to the beginning of the following month), then we reduce that date by a day (to bring it back to the end of the initial month).