skip to Main Content

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

3

Answers


  1. This work for me:

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

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

    Login or Signup to reply.
  2. 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');
    
    Login or Signup to reply.
  3. set date format to dmy (day – month – year )

    SET datestyle = dmy;
    

    This answer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search