postgresqlHow do I extract the year from a date in PostgreSQL?
The easiest way to extract the year from a date in PostgreSQL is to use the EXTRACT
function. This function takes a date field and a unit of time, and returns the corresponding value. For example, to extract the year from a date field called date_field
, the following query can be used:
SELECT EXTRACT(YEAR FROM date_field) FROM table_name;
This will return a list of years for each of the dates in the date_field
column.
The parts of this query are:
SELECT
- this is the clause used to indicate that data is being selected from a table.EXTRACT
- this is the function used to extract the year from a date field.YEAR
- this is the unit of time that is being extracted from the date field.FROM
- this is the clause used to indicate the table that the data is being selected from.
Helpful links
More of Postgresql
- How can I use PostgreSQL and ZFS snapshots together?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I use PostgreSQL's "zero if null" feature?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I use PostgreSQL with YAML?
- How do I parse XML data using PostgreSQL?
- How can I extract the year from a PostgreSQL timestamp?
- How do I use PostgreSQL's XMIN and XMAX features?
- How do I use a PostgreSQL XML parser in an example?
See more codes...