Search code examples
sqlpostgresqldata-sciencedata-analysis

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


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


Solution

  • 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');