SQL Tutorial SQL Advanced SQL Database SQL References

MySQL LAST_DAY() Function



The MySQL LAST_DAY() function returns the last day of the month for a given date/datetime value. If the datetime is not a valid date or datetime value, this function returns NULL.

Syntax

LAST_DAY(datetime)

Parameters

datetime Required. Specify a date or datetime value from which to extract the last day of the month.

Return Value

Returns the last day of the month for a given date/datetime value.

Example 1:

The example below shows the usage of LAST_DAY() function.

mysql> SELECT LAST_DAY('2018-08-18');
Result: '2018-08-31'

mysql> SELECT LAST_DAY('2018-08-18 10:38:42');
Result: '2018-08-31'

mysql> SELECT LAST_DAY('2018-08-18 10:38:42.000004');
Result: '2018-08-31'

mysql> SELECT LAST_DAY('2014-10-25');
Result: '2014-10-31'

mysql> SELECT LAST_DAY('Date is: 2014-10-25');
Result: NULL

mysql> SELECT LAST_DAY(CURDATE());
Result: '2021-12-31'

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42
1201.612018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-02-06 09:31:34

The statement given below can be used to get the last day of month specified by values of column OrderTime:

SELECT *, LAST_DAY(OrderTime) AS LAST_DAY_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeLAST_DAY_Value
1001.582017-08-18 10:38:422017-08-31
1201.612018-03-23 07:14:162018-03-31
1251.782018-09-12 05:25:562018-09-30
501.802019-01-16 11:52:052019-01-31
2001.722020-02-06 09:31:342020-02-29

❮ MySQL Functions