MySQL LAST_DAY Function

MySQL LAST_DAY Function

MySQL LAST_DAY Function

The LAST_DAY function in MySQL retrieves the last day of the month for a given date. This function is particularly useful when working with dates and performing calculations requiring knowledge of the month's end.

Syntax

LAST_DAY(date)
  • date: A valid date or date-time expression from which the last day of the month is derived.

How It Works

  1. If the provided date is valid, LAST_DAY returns the last day of the month as a DATE value.
  2. If the provided date is NULL, the function returns NULL.
  3. If an invalid date is provided, MySQL generates an error.

Examples

1. Get the Last Day of the Current Month

SELECT LAST_DAY(CURDATE()) AS last_day;

Output:
If the current date is 2025-01-26, the result would be:

+------------+ | last_day | +------------+ | 2025-01-31 | +------------+

2. Get the Last Day of a Specific Date

SELECT LAST_DAY('2025-02-10') AS last_day;

Output:

+------------+ | last_day | +------------+ | 2025-02-28 | +------------+

For February 2025, since it’s not a leap year, the last day is February 28.

3. Handle Invalid Dates

SELECT LAST_DAY('2025-02-30') AS last_day;

Output:
This will result in an error because 2025-02-30 is not a valid date.

4. Using LAST_DAY in a Query with a Table

Suppose you have an orders table:

order_idorder_date
12025-01-15
22025-02-05
32025-03-20

Query to get the last day of the month for each order date:

SELECT order_id, order_date, LAST_DAY(order_date) AS last_day_of_month FROM orders;

Output:

+----------+------------+------------------+ | order_id | order_date | last_day_of_month| +----------+------------+------------------+ | 1 | 2025-01-15 | 2025-01-31 | | 2 | 2025-02-05 | 2025-02-28 | | 3 | 2025-03-20 | 2025-03-31 | +----------+------------+------------------+

5. Use with Calculations

Get the number of days remaining in the current month:

SELECT DATEDIFF(LAST_DAY(CURDATE()), CURDATE()) AS days_remaining;

Output:
If the current date is 2025-01-26, the result would be:

+----------------+ | days_remaining | +----------------+ | 5 | +----------------+

Practical Use Cases

  1. Generate Monthly Reports: Retrieve the last day of each month to define report boundaries.
  2. Billing Cycles: Determine the end of a billing cycle for a given date.
  3. Date Validations: Use LAST_DAY to validate whether a specific date falls within the current month.

Notes

  • The LAST_DAY function works seamlessly with both DATE and DATETIME data types.
  • If used in conjunction with other date functions, it can simplify complex date-related queries.

Conclusion

The LAST_DAY function is a simple yet powerful tool in MySQL for working with the end of months. It helps streamline operations that involve dates and ensures accurate and efficient handling of month boundaries.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close