MySQL MONTH() Function
The MONTH() function in MySQL extracts the month (as a number) from a given date or datetime value. It returns an integer between 1 (January) and 12 (December).
Syntax
Parameter:
date: A validDATE,DATETIME, orTIMESTAMPvalue from which the month will be extracted.
Return Value:
- Returns an integer (1-12) representing the month.
- Returns
NULLif the input isNULLor an invalid date.
Examples
1. Extracting the Month from a Date
Output:
2. Extracting the Month from a Datetime
Output:
3. Using MONTH() on a Table Column
Assume we have a orders table:
This query will return the order month for each order.
4. Using MONTH() in a WHERE Clause
Find all records from July:
Handling NULL and Invalid Dates
Alternative: Getting the Month Name
If you need the month name instead of the number, use MONTHNAME():
Output:
Conclusion
MONTH()is useful for extracting the month as a number (1-12).- Can be used in
SELECT,WHERE, and other SQL clauses. - Use
MONTHNAME()if you need the full month name instead.
This function is great for filtering and grouping data based on months! š

