MySQL MONTH() Function

The MONTH() function is used to extract the month part from a given date or datetime value.


Definition and Usage

  • Purpose: Extract the month component from a date or datetime value.
  • Range: Returns a number from 1 (January) to 12 (December).

Syntax

MONTH(date)

Parameter Values

Parameter Description
date Required. The date or datetime to extract the month from.

Technical Details

  • Introduced in: MySQL 4.0 and later.
  • Return Type: An integer value between 1 and 12.

Examples

Example 1: Extracting Month from a Date

SELECT MONTH("2017-06-15");

Result: 6

Example 2: Extracting Month from a Datetime

SELECT MONTH("2017-06-15 09:34:21");

Result: 6

Example 3: Extracting Month from the Current Date

SELECT MONTH(CURDATE());

Result: The current month (depends on system date).

Example 4: Using MONTH() in a Query

SELECT * FROM orders WHERE MONTH(order_date) = 12;

Use Case: Select all orders placed in December.

Example 5: Invalid Input

SELECT MONTH("invalid-date");

Result: NULL
(Invalid date values return NULL.)


Key Notes

  1. The MONTH() function works with both DATE and DATETIME values.
  2. If the input value is NULL or invalid, the function returns NULL.
  3. Can be combined with other date functions for advanced queries, e.g., filtering records by month.

Practical Uses

  • Reports: Generate reports grouped by months.
  • Filters: Retrieve records for a specific month.
  • Analysis: Analyze trends and data patterns on a monthly basis.

Was this article helpful?