How to extract year and month from date in PostgreSQL ?

How to extract year and month from date in PostgreSQL ?

Asked on January 9, 2019 in PostgreSQL.
Add Comment


  • 3 Answer(s)

    Use this  Command :

    to_char(timestamp, 'YYYY-MM')
    
    

    I know the order is no correct.but i know  why it is wrong (at least until year 10000 comes around).

    Answered on January 9, 2019.
    Add Comment

    Extract year and month from date in PostgreSQL

    date_part(text, timestamp)
    
    

    Refer this link

    I know the order is no correct.but i know why it is wrong (at least until year 10000 comes around).

    Answered on January 9, 2019.
    Add Comment

    Use the  date_trunc method to truncate off the day

    Below is example command

    select
       SUM(amount) as sales,
       date_trunc('month', created_at) as date
    from orders
    group by date
    order by date DESC;
    
    Answered on January 9, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.