9951 explained code solutions for 126 technologies


postgresqlHow can I extract the year from a PostgreSQL timestamp?


To extract the year from a PostgreSQL timestamp, you can use the EXTRACT function. This function takes a field name and a timestamp as its arguments, and returns the specified field from the timestamp. The field name for the year is YEAR.

Here is an example of how to use the EXTRACT function to extract the year from a timestamp:

SELECT EXTRACT(YEAR FROM timestamp '2011-12-25 12:34:56');

This will return the following output:

2011

The code consists of the following parts:

  • SELECT - the keyword used to initiate a query
  • EXTRACT - the function used to extract a field from a timestamp
  • YEAR - the field name for the year
  • FROM - the keyword used to indicate the source of the timestamp
  • timestamp '2011-12-25 12:34:56' - the timestamp from which the year should be extracted

Helpful links

Edit this code on GitHub