MariaDB LAST_DAY Function

In MariaDB, LAST_DAY is a built-in function that returns the last day of the month for a given date.

MariaDB LAST_DAY Syntax

This is the syntax of the MariaDB LAST_DAY function:

LAST_DAY(date)

Parameters

date

Required. A date or datetime expression.

If you provide no parameters or the wrong number of parameters, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LAST_DAY'.

Return value

The MariaDB LAST_DAY function returns the last day of the month for the specified date.

If the argument is an invalid date or is NULL, the LAST_DAY function will return NULL.

MariaDB LAST_DAY Examples

Basic Example

This statement shows the basic usage of the MariaDB LAST_DAY function:

SELECT
    LAST_DAY('2020-02-01'),
    LAST_DAY('2021-02-01'),
    LAST_DAY('2022-02-01'),
    LAST_DAY('2023-02-01'),
    LAST_DAY('2022-02-01 10:11:12')\G

Output:

         LAST_DAY('2020-02-01'): 2020-02-29
         LAST_DAY('2021-02-01'): 2021-02-28
         LAST_DAY('2022-02-01'): 2022-02-28
         LAST_DAY('2023-02-01'): 2023-02-28
LAST_DAY('2022-02-01 10:11:12'): 2022-02-28

Digital date

The MariaDB LAST_DAY function allow you to pass dates as numbers, but you must provide a valid date.

SELECT
  LAST_DAY(20230101),
  LAST_DAY(230101);

Output:

+--------------------+------------------+
| LAST_DAY(20230101) | LAST_DAY(230101) |
+--------------------+------------------+
| 2023-01-31         | 2023-01-31       |
+--------------------+------------------+

Other delimiters

The MariaDB LAST_DAY() function allow you to construct dates with various separators:

SELECT
    LAST_DAY('2023/01/10'),
    LAST_DAY('2023,01!10'),
    LAST_DAY('2023#01%10');

Output:

+------------------------+------------------------+------------------------+
| LAST_DAY('2023/01/10') | LAST_DAY('2023,01!10') | LAST_DAY('2023#01%10') |
+------------------------+------------------------+------------------------+
| 2023-01-31             | 2023-01-31             | 2023-01-31             |
+------------------------+------------------------+------------------------+

Current date

We can pass NOW() as the parameter to get the last day of the current month:

SELECT
    NOW(),
    LAST_DAY(NOW());

Output:

+---------------------+-----------------+
| NOW()               | LAST_DAY(NOW()) |
+---------------------+-----------------+
| 2023-01-10 11:16:26 | 2023-01-31      |
+---------------------+-----------------+

Conclusion

In MariaDB, LAST_DAY is a built-in function that returns the last day of the month for a given date.