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:
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:
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 :
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 :
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:
id name salary start_date city region 2 Robert 14,420.00 1/2/1995 Vancouver N 3 Celia 24,020.00 12/3/1996 Toronto W 4 Linda 40,620.00 11/4/1997 New York N 5 David 80,026.00 10/5/1998 Vancouver W 6 James 70,060.00 9/6/1999 Toronto N 7 Alison 90,620.00 8/7/2000 New York W
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 :
name date_part Robert 1995 Celia 1996 Linda 1997 David 1998 James 1999 Alison 2000
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 :
name date_part Robert 1 Alison 8 James 9 David 10 Linda 11 Celia 12