In this tutorial, we will demonstrate briefly the MySQL LAST_DAY function with more useful examples and it’s syntax. We will take different examples of LAST_DAY function with MySQL like CURDATE(), NOW(), etc.
MySQL provides many dates or time functions here, you will learn LAST_DAY() function of MySQL With CURDATE() OR NOW() a date function.
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.
If you pass the (0000-00-00) value or invalid value in the LAST_DAY() function, it will return null, invalid, NULL.
Syntax
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
Now let’s take an example to explain.
SELECT LAST_DAY('2020-06-18') AS 'Result';
Output-1
+-------------+ | Result | +-------------+ | 2020-06-30 | +-------------+
Example-2
Let’s take another example to explain with a different date.
SELECT LAST_DAY('2018-02-01') AS 'Result';
Output-2
+-------------+ | Result | +-------------+ | 2018-02-28 | +-------------+
Example-3 Add Month
In this example, we will get the last day of next month using this function.
We will 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. See the query of the following :
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 Using Current Date/Time
Let’s take a new example, we will use the NOW() function with the LAST_DAY() function.
SELECT NOW(), LAST_DAY(NOW());
Output-4
+---------------------+--------------------+ | NOW() | LAST_DAY(NOW()) | +---------------------+--------------------+ | 2019-07-12 18:30:44 | 2019-07-31 | +---------------------+--------------------+
Example-4 With MySQL CURDATE() Function
Let’s take the next example of using CURDATE () function. If you want to know more about CURDATE () function click here.
SELECT CURDATE(), LAST_DAY(CURDATE());
Output-5
+------------+-----------------------+ | CURDATE() | LAST_DAY(CURDATE()) | +------------+-----------------------+ | 2019-07-12 | 2019-07-31 | +------------+-----------------------+
Example-6 Minus Month
In this example, we will get the last day of the previous month using this function.
We will minus one month in current date-time value and pass the value in LAST_DAY() function. It will return on the last day of the previous month. See the query of the following :
SELECT LAST_DAY(CURDATE() - INTERVAL 1 MONTH);
Output-6
+----------------------------------------+ | LAST_DAY(CURDATE() - INTERVAL 1 MONTH) | +----------------------------------------+ | 2019-06-30 | +----------------------------------------+ 1 row in set (0.00 sec)
Conclusion
Here, you have learned how to use MySQL LAST_DAY() function with various examples.