How to Use the MONTH() Function in MySQL

Contents
In this article, you will learn how to use the MONTH() function in MySQL.
Using the MONTH() function in MySQL
In MySQL, the MONTH() function is used to extract the month from a date. The function returns the month as an integer value, ranging from 1 to 12.
Syntax
MONTH(date);
where date is the input date in any valid date format.
Examples
Here are some examples of using the MONTH() function in MySQL:
Extracting the month from a date string
To extract the month from a date string, you can pass the string as an argument to the MONTH() function.
Syntax
SELECT MONTH('2022-05-15');
Example
SELECT MONTH('2022-05-15');
Output:
+-------------------+
| MONTH('2022-05-15') |
+-------------------+
| 5 |
+-------------------+
Extracting the month from a date column in a table
To extract the month from a date column in a table, you can use the MONTH() function in the SELECT statement.
Syntax
SELECT MONTH(date_column) FROM table_name;
Example
Consider a table orders with columns order_id, order_date, and order_total. To extract the month from the order_date column, you can use the following query:
SELECT MONTH(order_date) FROM orders;
Output:
+----------------+
| MONTH(order_date) |
+----------------+
| 1 |
| 4 |
| 5 |
| 8 |
+----------------+
Using MONTH() function with WHERE clause
You can also use the MONTH() function in a WHERE clause to filter records based on the month.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE MONTH(date_column) = month_value;
Example
To select orders placed in the month of May, you can use the following query:
SELECT order_id, order_total
FROM orders
WHERE MONTH(order_date) = 5;
Output:
+----------+-------------+
| order_id | order_total |
+----------+-------------+
| 1001 | 250.00 |
+----------+-------------+