MySQL MONTH Function: Get The Month Of A Specified Date

MySQL MONTH Function: Get The Month Of A Specified Date

The MONTH() function in MySQL is a date function that returns the month for a given date. The month is returned as an integer ranging from 1 to 12.

Syntax:

MONTH(date) 

Here, date is a valid date expression.

Example:

SELECT MONTH('2023-05-12'); 

This will return 5, because May is the 5th month of the year.

Usage in a Table:

Consider the following orders table:

OrderIDProductOrderDate
1Apples2023-05-12
2Bananas2023-06-15
3Oranges2023-07-20
4Apples2023-05-25
5Bananas2023-06-30

If you want to select all orders that were placed in May, you can use the MONTH() function in the WHERE clause:

SELECT * FROM orders WHERE MONTH(OrderDate) = 5; 

This will return all rows where OrderDate is in May:

OrderIDProductOrderDate
1Apples2023-05-12
4Apples2023-05-25

Usage with GROUP BY:

You can also use the MONTH() function with the GROUP BY clause to group orders by month. For example, if you want to count the number of orders placed in each month, you could do this:

SELECT MONTH(OrderDate) AS OrderMonth, COUNT(*) AS NumberOfOrders FROM orders GROUP BY OrderMonth; 

This will return a result set with the number of orders placed in each month.

Remember that the MONTH() function will return NULL if the input is not a valid date expression.

Examples

  1. MySQL MONTH Function Example:

    • Description: The MONTH function in MySQL is used to extract the month component from a date.
    • Example Code:
      SELECT MONTH(date_column) AS month_value FROM table_name; 
  2. How to Use MONTH Function in MySQL:

    • Description: Use the MONTH function to retrieve the month component from a date.
    • Example Code:
      SELECT MONTH('2023-04-15') AS month_value; 
  3. Getting the Month of a Date with MONTH in MySQL:

    • Description: MONTH is used to get the month part of a date, ranging from 1 to 12.
    • Example Code:
      SELECT order_id, MONTH(order_date) AS order_month FROM orders; 
  4. Extracting Month from Date Values in MySQL:

    • Description: Use the MONTH function to extract the month from date values in a table.
    • Example Code:
      SELECT employee_name, MONTH(hire_date) AS hire_month FROM employees; 
  5. MONTH Function with WHERE Clause in MySQL:

    • Description: Apply the MONTH function with a WHERE clause to filter records based on the month condition.
    • Example Code:
      SELECT customer_name, order_date FROM orders WHERE MONTH(order_date) = 6; -- Select orders in June 
  6. Handling NULL Values with MONTH Function in MySQL:

    • Description: The MONTH function handles NULL values gracefully. If the date is NULL, the result is also NULL.
    • Example Code:
      SELECT customer_name, MONTH(last_purchase_date) AS last_purchase_month FROM customers; 
  7. Examples of Using MONTH Function in MySQL Queries:

    • Description: Various examples showcasing the application of the MONTH function in different scenarios.
    • Example Code:
      SELECT product_name, MONTH(release_date) AS release_month FROM products; 
      SELECT employee_id, MONTH(birthdate) AS birth_month FROM employees WHERE MONTH(birthdate) = 12; -- Select employees born in December 
  8. Using MONTH with Date Formatting in MySQL:

    • Description: Combine MONTH with date formatting functions for customized output.
    • Example Code:
      SELECT order_id, CONCAT('Month ', MONTH(order_date)) AS formatted_month FROM orders; 

More Tags

angular-httpclient asp.net-membership mysql-error-1093 python-3.6 architecture google-chrome-extension c99 android-actionbar-compat splunk-query powershell-cmdlet

More Programming Guides

Other Guides

More Programming Examples