PostgreSQL – How to Get Month From a Date
To extract the month from a date in PostgreSQL, you can use the EXTRACT
function. Here’s an example:
SELECT EXTRACT(MONTH FROM your_date_column) AS month FROM your_table;
where your_date_column
is the name of your date column and your_table
is the name of your table.
In this query, EXTRACT(MONTH FROM your_date_column)
is used to extract the month component from the date in the specified column. It will return the numeric representation of the month (1 for January, 2 for February, and so on) for each date in the column.
For example, if you have a table called sales
with a date column named transaction_date
, you can retrieve the month for each transaction date like this:
SELECT EXTRACT(MONTH FROM transaction_date) AS month FROM sales;
This will give you a result set with the month for each transaction date in the sales
table.