Sunday, December 22, 2024

MySQL LAST_DAY() Function

 The LAST_DAY() function in MySQL returns the last day of the month for a given date or datetime value.

Syntax:

LAST_DAY(date)
  • date: The date (or datetime) for which you want to find the last day of the month. This can be a column, a date literal, or any valid date expression.

Example Usage:

  1. Finding the last day of a specific month:

    SELECT LAST_DAY('2024-12-23');
    

    This will return 2024-12-31, the last day of December 2024.

  2. Using LAST_DAY() with a column:

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

    This will return the last day of the month for each order_date in the orders table.

  3. Finding the last day of the current month:

    SELECT LAST_DAY(CURDATE());
    

    This will return the last day of the current month.

Notes:

  • The function will return the last day in the Gregorian calendar, meaning it adjusts for leap years (e.g., February 29 for leap years).
  • The argument for LAST_DAY() should be a valid date or datetime expression.

No comments:

Post a Comment