w3resource

MySQL LAST_DAY() function

LAST_DAY() function

MySQL LAST_DAY() returns the last day of the corresponding month for a date or datetime value. If the date or datetime value is invalid, the function returns NULL. It helps determine the final day of a month, which is particularly useful for date calculations, reporting, and analysis.

This function is useful in -

  • LAST_DAY() is valuable for calculating the last day of a month, which is useful for various financial and planning calculations.
  • For calculating ages based on birthdates, LAST_DAY() helps determine the end of the month to calculate the difference in years accurately.
  • LAST_DAY() is useful for time-based data analysis, allowing you to align data with month-end points for analysis and visualization.
  • The function is valuable for financial planning, budgeting, and forecasting, as it aids in determining month-end positions.
  • LAST_DAY() can be used to generate custom date formats that include the last day of the month.
  • In graphical presentations, LAST_DAY() can be used to label axes or data points with month-end values.

Syntax:

LAST_DAY(date1)

Where date1 is a date.

Syntax Diagram:

MySQL LAST_DAY() Function - Syntax Diagram

MySQL Version: 8.0

Pictorial Presentation:

Pictorial Presentation of MySQL LAST_DAY() function

Example: MySQL LAST_DAY() function

The following statement will return the last date of the corresponding month of the given date 2009-05-18.

Code:

SELECT LAST_DAY('2009-05-18');

Output:

mysql> SELECT LAST_DAY('2009-05-18');
+------------------------+
| LAST_DAY('2009-05-18') |
+------------------------+
| 2009-05-31             | 
+------------------------+
1 row in set (0.00 sec)

Example : LAST_DAY() function with datetime

The following statement will return the last date of the corresponding month of the given datetime 2009-02-18 15:45:53.

Code:

SELECT LAST_DAY('2009-02-18 15:45:53');

Output:

mysql> SELECT LAST_DAY('2009-02-18 15:45:53');
+---------------------------------+
| LAST_DAY('2009-02-18 15:45:53') |
+---------------------------------+
| 2009-02-28                      | 
+---------------------------------+
1 row in set (0.00 sec)

Video Presentation:

All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: HOUR()
Next: LOCALTIME()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/date-and-time-functions/mysql-last_day-function.php