15 posts
The LAST_DAY() function in MySQL can be used to know the last day of the month for a given date or a datetime. The LAST_DAY() function takes a date value as argument and returns the last day of month in that date. The date argument represents a valid date or datetime.
LAST_DAY(Date);
If the date or datetime value is invalid, the function returns NULL.
Date: The LAST_DAY() function takes a single argument Date. It is the date or datetime value for which you want to extract the last day of the month.
The LAST_DAY() function returns the last day of the month for a valid Date argument. If the argument Date is invalid or null, then the function will also return NULL.
To know the last date of the month January 2019,the LAST_DAY() function can be executed in this way:
SELECT LAST_DAY('2019-01-25');
Output :
'2019-01-31'
To know the last date of the month January using datetime format,the LAST_DAY() function can be executed in this way:
SELECT LAST_DAY('2019-01-25 08:20:05');
Output :
'2019-01-31'
To know whether the year is a leap year or not, we can use the LAST_DAY() function to check the last day of the month February of that year. If it is the 29th day then that year is leap otherwise not.
SELECT LAST_DAY('2016-02-15');
Output :
'2016-02-29'
To know the last date of the current month ,the LAST_DAY() function is combined with the NOW() or CURDATE() function and can be executed in this way:
Using the NOW() function:
The NOW() function in MySQL returns the current date-time stamp.
SELECT LAST_DAY(NOW());
Output :
'2019-07-31'
SELECT LAST_DAY(CURDATE());
Output :
'2019-07-31'
To know the last date of the next month,the last_day() function can be executed in this way:
SELECT LAST_DAY(CURDATE() + INTERVAL 1 MONTH);
Output :
'2019-08-31'
Please log in to leave a comment.