Skip to content
Advertisement

How to extract year and month from date in PostgreSQL without using to_char() function?

I want to select sql: SELECT "year-month" from table group by "year-month" AND order by date, where year-month – format for date “1978-01″,”1923-12”. select to_char of couse work, but not “right” order:

to_char(timestamp_column, 'YYYY-MM')

Advertisement

Answer

date_part(text, timestamp)

e.g.

date_part('month', timestamp '2001-02-16 20:38:40'),
date_part('year', timestamp '2001-02-16 20:38:40') 

http://www.postgresql.org/docs/8.0/interactive/functions-datetime.html

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement