How to Extract Year from DATE in POSTGRESQL How to Extract Year from DATE in POSTGRESQL postgresql postgresql

How to Extract Year from DATE in POSTGRESQL


Try

select date_part('year', your_column) from your_table;

or

select extract(year from your_column) from your_table;


answer is;

select date_part('year', timestamp '2001-02-16 20:38:40') as year,       date_part('month', timestamp '2001-02-16 20:38:40') as month,       date_part('day', timestamp '2001-02-16 20:38:40') as day,       date_part('hour', timestamp '2001-02-16 20:38:40') as hour,       date_part('minute', timestamp '2001-02-16 20:38:40') as minute


This line solved my same problem in postgresql:

SELECT DATE_PART('year', column_name::date) from tableName;

If you want month, then simply replacing year with month solves that as well and likewise.