Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp Difference In Hours for PostgreSQL

Is there a TIMESTAMPDIFF() equivalent for PostgreSQL?

I know I can subtract two timestamps to get a postgresql INTERVAL. I just want the difference between the two timestamps in in hours represented by an INT.

I can do this in MySQL like this:

TIMESTAMPDIFF(HOUR, links.created, NOW()) 

I just need the difference between two timestamps in hours represented as an integer.

Solution works for me:

SELECT "links_link"."created", "links_link"."title", (EXTRACT(EPOCH FROM current_timestamp - "links_link"."created")/3600)::Integer AS "age"  FROM "links_link" 
like image 996
TheLizardKing Avatar asked Dec 26 '09 22:12

TheLizardKing


People also ask

How do I calculate day difference in PostgreSQL?

To count the difference between dates as days in PostgreSQL or Oracle, you simply need to subtract one date from the other, e.g. arrival - departure . But in most cases, what you really want is the number of days from the first date to the second date inclusively.

How do you find the difference between two timestamps?

If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

How can I find the difference between two timestamps in SQL?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR .

What is the format of timestamp in PostgreSQL?

Postgres DATE data type Postgres uses the DATE data type for storing different dates in YYYY-MM-DD format. It uses 4 bytes for storing a date value in a column. You can design a Postgres table with a DATE column and use the keyword DEFAULT CURRENT_DATE to use the current system date as the default value in this column.


2 Answers

The first things popping up

EXTRACT(EPOCH FROM current_timestamp-somedate)/3600 

May not be pretty, but unblocks the road. Could be prettier if division of interval by interval was defined.

Edit: if you want it greater than zero either use abs or greatest(...,0). Whichever suits your intention.

Edit++: the reason why I didn't use age is that age with a single argument, to quote the documentation: Subtract from current_date (at midnight). Meaning you don't get an accurate "age" unless running at midnight. Right now it's almost 1am here:

select age(current_timestamp);        age         ------------------  -00:52:40.826309 (1 row) 
like image 170
Michael Krelin - hacker Avatar answered Sep 30 '22 06:09

Michael Krelin - hacker


Get fields where a timestamp is greater than date in postgresql:

SELECT * from yourtable  WHERE your_timestamp_field > to_date('05 Dec 2000', 'DD Mon YYYY'); 

Subtract minutes from timestamp in postgresql:

SELECT * from yourtable  WHERE your_timestamp_field > current_timestamp - interval '5 minutes' 

Subtract hours from timestamp in postgresql:

SELECT * from yourtable  WHERE your_timestamp_field > current_timestamp - interval '5 hours' 
like image 40
Eric Leschinski Avatar answered Sep 30 '22 07:09

Eric Leschinski