I found exactly what I need in MySQL's TIMESTAMPDIFF()
but I am having a hell of a time trying to find an equivablent with PostgreSQL. I know I can subtract the two timestamps to get an INTERVAL
but I am having a hard time wrapping my head about what it means and specifically I just want the difference between the two timestamps in in hours represented by an INT. I accomplished this in MySQL by using:
TIMESTAMPDIFF(HOUR, links.created, NOW())
I am still learning PostgreSQL but so far I am really enjoying it. I have just hit a small roadblock with this. Thanks in advanced!
EDIT
These answers are close but I just need the difference between two timestamps in hours represented as an integer. Basically the only thing it should ever return is a 0 or a whole number > 0 representing the difference in hours.
EDIT 2
"2009-12-25 23:04:24.032883-05";"By The Book Brewing";"00:55:35.967117"
"2009-12-26 00:07:43.281076-05";"Sweetwater Brewery... Really bad site...";"-00:07:43.281076"
"2009-12-26 00:08:46.234226-05";"Best Domestic, Widely Available Beer";"-00:08:46.234226"
"2009-12-26 15:01:49.930634-05";"Radiohead";"-15:01:49.930634"
Is my output with a query of
SELECT "links_link"."created", "links_link"."title", AGE("links_link"."created") AS "age" FROM "links_link"
Why do I get both positive and negative numbers when age should be the difference between the given timestamp and a current timestamp?
EDIT 3
Haha, why would anyone put up with me? The different between two timestamps in hours. I'll give a more precise example. A rows creation timestamp and the current timestamp. The value should never be negative (unless row was inserted into the future) and if it was submitted less than an hour ago, the value should be 0. If something was submitted more exactly two days and 1 hour ago the returned value should be 49 (24 +24 + 1).
SOLUTION
After everybody's help, this is what I came up with:
SELECT "links_link"."created", "links_link"."title", (EXTRACT(EPOCH FROM current_timestamp - "links_link"."created")/3600)::Integer AS "age" FROM "links_link"
Time for the hard parts now :)