SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The LAST_DAY()
function in MySQL returns the last day of the specified month for the provided date. It's useful when you want to determine the end date of a month for a given date.
LAST_DAY(date)
date
: The date or datetime expression from which you want to find the last day of the month.Getting the Last Day of the Current Month:
SELECT LAST_DAY(NOW());
Getting the Last Day of a Specific Date:
SELECT LAST_DAY('2023-02-15');
This would return 2023-02-28
or 2023-02-29
depending on whether 2023 is a leap year or not.
Getting the Last Day of a Month from a Table Column:
Assume you have a table sales
with a column sale_date
. To get the last day of the month for each sale date, you'd do:
SELECT sale_date, LAST_DAY(sale_date) as last_day_of_month FROM sales;
The returned date will always be in the format YYYY-MM-DD
, regardless of the input date's format.
If the provided date is invalid or NULL
, the LAST_DAY()
function will return NULL
.
The LAST_DAY()
function is particularly useful in financial applications, reporting, and other scenarios where month-end calculations or reporting periods are necessary.
MySQL LAST_DAY() function example:
LAST_DAY()
function in MySQL is used to return the last day of the month for a given date.SELECT LAST_DAY('2023-04-15') AS LastDayOfMonth;
How to use LAST_DAY() function in MySQL:
LAST_DAY()
, provide a date as an argument, and it will return the last day of that month.SELECT LAST_DAY(NOW()) AS LastDayOfCurrentMonth;
MySQL get last day of month:
LAST_DAY()
to get the last day of the month.SELECT LAST_DAY('2023-08-21') AS LastDayOfMonth;
Extract month end date in MySQL:
LAST_DAY()
.SELECT LAST_DAY('2023-11-10') AS MonthEndDate;
MySQL date functions LAST_DAY() usage:
LAST_DAY()
in date functions.SELECT LAST_DAY(NOW()) AS LastDay, MONTH(NOW()) AS CurrentMonth;
Calculate end of month in MySQL:
LAST_DAY()
.SELECT LAST_DAY('2023-05-12') AS MonthEnd;
MySQL last day of current month:
SELECT LAST_DAY(NOW()) AS LastDayOfCurrentMonth;
Using LAST_DAY() in WHERE clause MySQL:
LAST_DAY()
in a WHERE
clause.SELECT * FROM your_table WHERE your_date_column = LAST_DAY('2023-07-15');
Get last day of quarter in MySQL:
LAST_DAY()
to get the last day of the quarter.SELECT LAST_DAY('2023-09-15' + INTERVAL QUARTER('2023-09-15') QUARTER) AS LastDayOfQuarter;
MySQL LAST_DAY() function vs other date functions:
LAST_DAY()
with other date functions like CURDATE()
.SELECT LAST_DAY(NOW()) AS LastDay, CURDATE() AS CurrentDate;
Examples of MySQL date manipulation with LAST_DAY():
LAST_DAY()
.SELECT LAST_DAY(NOW()) AS LastDay, LAST_DAY(NOW() - INTERVAL 1 MONTH) AS LastDayOfPreviousMonth;
MySQL extract year and month from date with LAST_DAY():
LAST_DAY()
.SELECT YEAR(LAST_DAY(NOW())) AS CurrentYear, MONTH(LAST_DAY(NOW())) AS CurrentMonth;
MySQL LAST_DAY() with timestamp:
LAST_DAY()
with a timestamp instead of a date.SELECT LAST_DAY(TIMESTAMP('2023-12-29 12:34:56')) AS LastDay;