How do I know how many hours have passed between the value of Timestamp and the present in Postgresql?

Asked 1 years ago, Updated 1 years ago, 78 views

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

2022-09-21 21:24

1 Answers

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


2022-09-21 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.