tags:

views:

36

answers:

2

When I subtract timestamps, the interval is in form DD:HH:MM:SS. How can I convert it all to minutes without extracting days and hours and multiplication/addition? I'm looking for a single function which I can substitute for date_part in this query so that it returns 65:

select date_part('minutes', '65 minutes'::interval);

Context: I need to know how many minutes have passed since given timestamp.

A: 

I don't actually think you can without doing a bunch of weirdness (like to_char) because minutes roll over at 59. Which is why you get:

postgres=# select date_part('minutes', '65 minutes'::interval);

date_part

     5

(1 row)

postgres=# select '65 minutes'::interval postgres-# ;

interval

01:05:00 (1 row)

Joshua D. Drake
Thanks. I know what problem I have, I'm looking for the most elegant solution.
Konrad Garus
A: 
SELECT EXTRACT(EPOCH FROM '2 months 3 days 12 hours 65 minutes'::INTERVAL)/60;

seems to work.

WARNING: "seems" is the key word.

Milen A. Radev
Yes it does. "epoch" returns number of seconds. Thanks! http://www.postgresql.org/docs/8.1/static/functions-datetime.html
Konrad Garus