Re: Elapsed time between timestamp variables in Function

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: Nico Callewaert <callewaert(dot)nico(at)telenet(dot)be>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Elapsed time between timestamp variables in Function
Date: 2009-02-04 22:38:00
Message-ID: 498A18C8.6070706@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 04/02/2009 21:59, Nico Callewaert wrote:

> To keep it simple, I have 2 variables, let's say A and B, both
> TimeStamp. Now I would like to know the absolute value of elapsed
> seconds between the 2 timestamps. Has to be absolute value, because
> can be positive or negative, depends if A > B or A < B. I tried with
> age(A, B), but that gives me something like 00:00:01, not really
> numeric value for number of seconds.

I had to do something similar recently (interval to minutes), and rolled
my own:

create or replace function
interval_to_minutes(interval)
returns integer
as
$$
select
cast(
(
extract(hour from $1) * 60
+ extract(minute from $1)
) as integer
);
$$
language sql stable;

Then you can do:

select abs(interval_to_minutes(A - B));

HTH,

Ray.

------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod(at)iol(dot)ie
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Grant Allen 2009-02-04 23:01:35 Re: Pet Peeves?
Previous Message Adam Rich 2009-02-04 22:18:23 Re: ramblings about password exposure (WAS: field with Password)