Home > Mobile >  How do I extract the month from the Date column in SQL-postgres
How do I extract the month from the Date column in SQL-postgres

Time:09-17

I am trying to extract the month from the the Order_date column by using postgres SQL , and the format is required to come in the name of the month format ;for example, December.

When I applied the extract function , It gave me an error message saying that the date setting has to be changed.

Please advise how to extract the month from the mentioned column ?

The data has been enclosed

SELECT EXTRACT(MONTH FROM order_date::date)
FROm think_sales;

The error message :

[22008] ERROR: date/time field value out of range: "25/06/2021" Hint: Perhaps you need a different "datestyle" setting.

Data :

enter image description here

CodePudding user response:

You may need to define your date format using the to_date function:

select to_char(to_date('25/06/2021', 'dd/mm/yyyy'), 'Month');

Output: June

In your case, you would use:

select to_char(to_date(order_date, 'dd/mm/yyyy'), 'Month');

CodePudding user response:

This work for me:

SELECT EXTRACT(MONTH FROM TIMESTAMP '25/06/2021');

https://www.postgresqltutorial.com/postgresql-date-functions/postgresql-extract/

CodePudding user response:

set date format to dmy (day - month - year )

SET datestyle = dmy;

This answer

  • Related