-->
julis

Get Year, Month in PostgreSQL

This example will show you how to get year, month using date_part from table in PostgreSQL database.
The date_part function is the traditional Postgres equivalent to the SQL-function extract:
date_part('field', source)

Note that here the field value needs to be a string. The valid field values for date_part are the same as for extract.
to see what valid field values for date_part click here
The table is:



























































idnamesalarystart_datecityregion
2Robert14,420.001/2/1995VancouverN
3Celia24,020.0012/3/1996TorontoW
4Linda40,620.0011/4/1997New YorkN
5David80,026.0010/5/1998VancouverW
6James70,060.009/6/1999TorontoN
7Alison90,620.008/7/2000New YorkW


Get year from start_date field for each name using :

SELECT name, date_part('year', start_date)

FROM employee

ORDER BY date_part;

Result is :































namedate_part
Robert1995
Celia1996
Linda1997
David1998
James1999
Alison2000


Get month from start_date for each name field using :

SELECT name, date_part('year', start_date)

FROM employee

ORDER BY date_part;

and the result is :































namedate_part
Robert1
Alison8
James9
David10
Linda11
Celia12

julis
julis
Load comments