MySQL LAST_DAY function or method; In this tutorial, i am going to show you MySQL LAST_DAY function with the help of examples.
MySQL LAST_DAY() Function
The MySQL LAST_DAY() is used to returns the last day of the month from a given date. We can pass the DATE OR DATETIME value in this function takes a DATE or DATETIME value.
Syntax of MySQL LAST_DAY() Function
Basic Syntax of LAST_DAY() function is:
LAST_DAY(date)
The date here is the date value that you want the day of the year from which it was returned.
Example 1 – MySQL LAST_DAY() Function
Let’s take an example using MySQL LAST_DAY() function; as follows:
SELECT LAST_DAY('2020-06-18') AS 'Result';
Output-1
+-------------+ | Result | +-------------+ | 2020-06-30 | +-------------+
Example 2 – MySQL LAST_DAY() Function
Let’s take an second new example using MySQL LAST_DAY() Function; as follows:
SELECT LAST_DAY('2018-02-01') AS 'Result';
Output-2
+-------------+ | Result | +-------------+ | 2018-02-28 | +-------------+
Example 3 – Add Month in MySQL LAST_DAY()
To add one month in current date-time value and pass the value in LAST_DAY() function. It will return on the last day of next month; as follows:
SELECT LAST_DAY(CURDATE() + INTERVAL 1 MONTH);
Output-3
+----------------------------------------+ | LAST_DAY(CURDATE() + INTERVAL 1 MONTH) | +----------------------------------------+ | 2019-08-31 | +----------------------------------------+ 1 row in set (0.00 sec)
Example 4 – MySQL LAST_DAY() Function
Let’s take an example using MySQL LAST_DAY() with now() function; as follows:
SELECT NOW(), LAST_DAY(NOW());
Output-4
+---------------------+--------------------+ | NOW() | LAST_DAY(NOW()) | +---------------------+--------------------+ | 2019-07-12 18:30:44 | 2019-07-31 | +---------------------+--------------------+
Example 5- MySQL LAST_DAY() Function
Let’s take an example using MySQL LAST_DAY() with curdate(); as follows:
SELECT CURDATE(), LAST_DAY(CURDATE());
Output-5
+------------+-----------------------+ | CURDATE() | LAST_DAY(CURDATE()) | +------------+-----------------------+ | 2019-07-12 | 2019-07-31 | +------------+-----------------------+
Conclusion
MySQL LAST_DAY() Function , you have learned how to use MySQL LAST_DAY() function with various examples.