Re: How to find number of seconds between 2 timestamps

From: Richard Huxton <dev(at)archonet(dot)com>
To: Woody Woodring <george(dot)woodring(at)iglass(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: How to find number of seconds between 2 timestamps
Date: 2009-02-17 15:30:44
Message-ID: 499AD824.7040307@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Woody Woodring wrote:
> I am trying to calculate a rate per second and am having trouble getting the
> number of seconds between the two timestamps to use in the formula.
>
> overview=> select extract(epoch from interval '1 day'::interval);
> date_part
> -----------
> 86400
> (1 row)
>
> overview=> select extract(epoch from interval ('2009-02-16
> 22:15:28.034567-06'::timestamp with time zone - '2009-02-15
> 22:15:28.034567-06'::timestamp with time zone));
> ERROR: syntax error at or near "'2009-02-16 22:15:28.034567-06'"

The problem is with "interval (...)" - you can say "interval 'constant'"
or "(expression)::interval".

In your case, since you are subtracting two timestamptz types you will
automatically get an interval type. So, you don't need to do anything:

select extract(epoch from ('2009-02-16 22:15:28.034567-06'::timestamp
with time zone - '2009-02-15 22:15:28.034567-06'::timestamp with time
zone));
date_part
-----------
86400
(1 row)

HTH

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas 2009-02-17 15:32:14 Re: Funtion to clean up strings?
Previous Message Woody Woodring 2009-02-17 15:08:56 How to find number of seconds between 2 timestamps