How to Extract Date Components from Timestamps in PostgreSQL

loading views

In PostgreSQL you can extract date components, like year, month, hour and minute from timestamps using the EXTRACT(component FROM date) function.

Input

SELECT
    extract(year FROM NOW()) AS year,
    extract(month FROM NOW()) AS month,
    extract(day FROM NOW()) AS day,
    extract(hour FROM NOW()) AS hour,
    extract(minute FROM NOW()) AS minute,
    extract(second FROM NOW()) AS second;

Output

yearmonthdayhourminutesecond
2022122270521

Source