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's "zero if null" feature?
- How do I use PostgreSQL with Qt?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How do I export data from PostgreSQL to an XML file?
- How do I set the PostgreSQL work_mem parameter?
- How do I get the version of PostgreSQL installed on my system?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I set a PostgreSQL interval to zero?
- How can I insert an empty array into a PostgreSQL database?
See more codes...