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) to12(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
1and12.
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
- The
MONTH()function works with bothDATEandDATETIMEvalues. - If the input value is
NULLor invalid, the function returnsNULL. - 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.