Sunday, December 22, 2024

MySQL MONTH() Function

 The MONTH() function in MySQL is used to extract the month from a given date or datetime expression. It returns an integer value between 1 and 12, where 1 represents January and 12 represents December.

Syntax:

MONTH(date)
  • date: The date or datetime expression from which you want to extract the month.

Example:

  1. Extract the month from a date:

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

    Output:

    12
    
  2. Extract the month from the current date:

    SELECT MONTH(CURRENT_DATE);
    

    Output: If today is December 23, 2024, the output will be:

    12
    
  3. Using MONTH() with a column: Assuming you have a sales table with a sale_date column:

    SELECT MONTH(sale_date) AS month FROM sales;
    

This function is useful when you want to perform operations based on the month, such as grouping data by month or filtering results.

No comments:

Post a Comment