I am using Postgresql DB. I want to read the created_at column value and check how many hours of row it was created.
select EXTRACT(HOUR FROM (NOW() - created_at)) as age
from table
The prices are coming out weird like this.
postgresql timestamp
This is because the EXTRACT function only takes the part corresponding to the time from the difference between the current time and created_at. For example, if the difference is 20 hours and 35 minutes a day, it's 20.
To make a difference from the current time, you can:
SELECT (EXTRACT(EPOCH FROM NOW() - created_at)/3600)::Integer AS hours
FROM table
Note: http://www.postgresql.org/docs/current/static/functions-datetime.html
© 2024 OneMinuteCode. All rights reserved.