Extracting the month from a date in SQL can be accomplished using various functions depending on the specific database management system (DBMS) you are using. Here, we will explore how to do this in some of the most popular DBMS, including MySQL, PostgreSQL, Microsoft SQL Server, and Oracle.
MySQL

In MySQL, you can extract the month from a date using the MONTH()
function. This function takes a date as an argument and returns the month of the date as a number (in the range 1-12).
SELECT MONTH(date_column) AS month
FROM your_table;
Example
Suppose you have a table named orders
with a column order_date
of type DATE
, and you want to extract the month from this date:
CREATE TABLE orders (
id INT,
order_date DATE
);
INSERT INTO orders (id, order_date)
VALUES (1, '2022-07-15');
SELECT MONTH(order_date) AS month
FROM orders;
This will return `7`, which is the month of July.
PostgreSQL

In PostgreSQL, you can use the EXTRACT()
function to extract the month from a date. The EXTRACT()
function takes two arguments: the field to extract (in this case, MONTH
), and the date from which to extract the field.
SELECT EXTRACT(MONTH FROM date_column) AS month
FROM your_table;
Example
Using the same table and data as the MySQL example:
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
order_date DATE
);
INSERT INTO orders (order_date)
VALUES ('2022-07-15');
SELECT EXTRACT(MONTH FROM order_date) AS month
FROM orders;
This will also return `7`, indicating the month of July.
Microsoft SQL Server
In Microsoft SQL Server, you can extract the month from a date using the MONTH()
function, similar to MySQL.
SELECT MONTH(date_column) AS month
FROM your_table;
Example
Again, using the same table and data:
CREATE TABLE orders (
id INT,
order_date DATE
);
INSERT INTO orders (id, order_date)
VALUES (1, '2022-07-15');
SELECT MONTH(order_date) AS month
FROM orders;
This query will return `7`, representing the month of July.
Oracle
In Oracle, you can extract the month from a date using the EXTRACT()
function, similar to PostgreSQL.
SELECT EXTRACT(MONTH FROM date_column) AS month
FROM your_table;
Example
Using the same data as before:
CREATE TABLE orders (
id NUMBER,
order_date DATE
);
INSERT INTO orders (id, order_date)
VALUES (1, '2022-07-15');
SELECT EXTRACT(MONTH FROM order_date) AS month
FROM orders;
This will return `7`, indicating July as the month.
DBMS | Function | Example |
---|---|---|
MySQL | MONTH(date) | MONTH('2022-07-15') |
PostgreSQL | EXTRACT(MONTH FROM date) | EXTRACT(MONTH FROM '2022-07-15') |
Microsoft SQL Server | MONTH(date) | MONTH('2022-07-15') |
Oracle | EXTRACT(MONTH FROM date) | EXTRACT(MONTH FROM '2022-07-15') |

Key Points
- The `MONTH()` function is used in MySQL and Microsoft SQL Server to extract the month from a date.
- The `EXTRACT(MONTH FROM date)` function is used in PostgreSQL and Oracle to achieve the same result.
- The month is returned as a numerical value (1-12) representing the respective month of the year.
- These functions can be used in various SQL queries for data analysis, filtering, or calculation purposes.
- Understanding the specific SQL function to use depends on the DBMS you are working with.
For more complex date manipulations or to extract other components of a date (like the year or day of the week), you can explore additional functions provided by your DBMS, such as `YEAR()`, `DAY()`, or `WEEKDAY()`, adapting the approach to your specific requirements and database system.
What is the difference between the MONTH() function and the EXTRACT() function for date manipulation in SQL?
+The MONTH()
function is specifically designed to extract the month from a date and is typically used in MySQL and Microsoft SQL Server. The EXTRACT()
function, on the other hand, is a more versatile function that can extract various components from a date (like the year, month, day, etc.) and is commonly used in PostgreSQL and Oracle. Both functions achieve similar results but have different syntax and application depending on the DBMS.
Can I use these functions to extract the month from a string that represents a date?
+Yes, but the string must be in a format that the DBMS can recognize as a date. The specific format may vary depending on the DBMS and its configuration. It’s often necessary to convert the string to a date type using a function like CAST()
or CONVERT()
before applying the MONTH()
or EXTRACT()
function.
How do I handle dates that are not in the standard format (YYYY-MM-DD) in SQL?
+When dealing with non-standard date formats, you may need to use specific functions provided by your DBMS to convert the date string into a recognizable date format. For example, in MySQL, you can use the STR_TO_DATE()
function, while in PostgreSQL, you might use the TO_DATE()
function. Always refer to the documentation of your specific DBMS for the most accurate and efficient method.