MySQL LAST_DAY

The MySQL LAST_DAY function is a date function that returns the last day of the month for a given date or date expression. It is particularly useful when you want to find the end date of a specific month.

Syntax

The syntax for the LAST_DAY function is as follows:

LAST_DAY(date)

Here, “date” is the input date or date expression for which you want to determine the last day of the month.

Example

Let’s look at an example:

SELECT LAST_DAY('2024-01-15') AS LastDayOfMonth;

In this example, the LAST_DAY function is applied to the date ‘2024-01-15’. The result would be:

+----------------+
| LastDayOfMonth |
+----------------+
| 2024-01-31     |
+----------------+

As shown in the result, the LAST_DAY function has returned the last day of the month for the input date, which is January 31, 2024.

You can also use the LAST_DAY function in combination with other date functions or in WHERE clauses to filter data based on the last day of the month.

For example:

SELECT *
FROM your_table
WHERE date_column = LAST_DAY(NOW());

This query retrieves all records from the table “your_table” where the “date_column” is equal to the last day of the current month.

In summary, the MySQL LAST_DAY function is a convenient tool for working with date data, allowing you to easily determine the last day of a month for a given date or date expression.