SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

MySQL | LAST_DAY() Function

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.

Syntax:

LAST_DAY(date)
  • date: The date or datetime expression from which you want to find the last day of the month.

Examples:

  1. Getting the Last Day of the Current Month:

    SELECT LAST_DAY(NOW());
    
  2. 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.

  3. 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;
    

Notes:

  • 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.

  1. MySQL LAST_DAY() function example:

    • The 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;
    
  2. How to use LAST_DAY() function in MySQL:

    • To use LAST_DAY(), provide a date as an argument, and it will return the last day of that month.
    SELECT LAST_DAY(NOW()) AS LastDayOfCurrentMonth;
    
  3. MySQL get last day of month:

    • Use LAST_DAY() to get the last day of the month.
    SELECT LAST_DAY('2023-08-21') AS LastDayOfMonth;
    
  4. Extract month end date in MySQL:

    • Extracting the month-end date using LAST_DAY().
    SELECT LAST_DAY('2023-11-10') AS MonthEndDate;
    
  5. MySQL date functions LAST_DAY() usage:

    • Demonstrating the usage of LAST_DAY() in date functions.
    SELECT LAST_DAY(NOW()) AS LastDay, MONTH(NOW()) AS CurrentMonth;
    
  6. Calculate end of month in MySQL:

    • Calculating the end of the month using LAST_DAY().
    SELECT LAST_DAY('2023-05-12') AS MonthEnd;
    
  7. MySQL last day of current month:

    • Getting the last day of the current month.
    SELECT LAST_DAY(NOW()) AS LastDayOfCurrentMonth;
    
  8. Using LAST_DAY() in WHERE clause MySQL:

    • Using LAST_DAY() in a WHERE clause.
    SELECT * FROM your_table WHERE your_date_column = LAST_DAY('2023-07-15');
    
  9. Get last day of quarter in MySQL:

    • Using LAST_DAY() to get the last day of the quarter.
    SELECT LAST_DAY('2023-09-15' + INTERVAL QUARTER('2023-09-15') QUARTER) AS LastDayOfQuarter;
    
  10. MySQL LAST_DAY() function vs other date functions:

    • Comparing LAST_DAY() with other date functions like CURDATE().
    SELECT LAST_DAY(NOW()) AS LastDay, CURDATE() AS CurrentDate;
    
  11. Examples of MySQL date manipulation with LAST_DAY():

    • Examples showcasing various date manipulations using LAST_DAY().
    SELECT 
      LAST_DAY(NOW()) AS LastDay, 
      LAST_DAY(NOW() - INTERVAL 1 MONTH) AS LastDayOfPreviousMonth;
    
  12. MySQL extract year and month from date with LAST_DAY():

    • Extracting the year and month from a date using LAST_DAY().
    SELECT 
      YEAR(LAST_DAY(NOW())) AS CurrentYear, 
      MONTH(LAST_DAY(NOW())) AS CurrentMonth;
    
  13. MySQL LAST_DAY() with timestamp:

    • Using LAST_DAY() with a timestamp instead of a date.
    SELECT LAST_DAY(TIMESTAMP('2023-12-29 12:34:56')) AS LastDay;