How to Extract Date Components from Timestamps in PostgreSQL
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
year | month | day | hour | minute | second |
---|---|---|---|---|---|
2022 | 12 | 22 | 7 | 05 | 21 |