Re: Interval Question

From: Timothy Perrigo <tperrigo(at)wernervas(dot)com>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Interval Question
Date: 2005-01-11 15:10:48
Message-ID: F99D0261-63E2-11D9-95C7-000A95C4F0A2@wernervas.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Can you post the code for the function you are having trouble with?
The following psql query works as expected (returns a negative
interval):

select '3 days 4 hours 17 mins'::interval - '3 days 6 hours 17
mins'::interval;
?column?
-----------
-02:00:00

Inserting the difference into a table with an interval column also
seems to work:

azrael=# create temp table interval_test(dif interval);
CREATE TABLE
azrael=# insert into interval_test select '3 days 4 hours 17
mins'::interval - '3 days 6 hours 17 mins'::interval;
INSERT 13615943 1
azrael=# select * from interval_test;
dif
-----------
-02:00:00
(1 row)

On Jan 11, 2005, at 8:34 AM, Terry Lee Tucker wrote:

> Greetings:
>
> I am working on a function which returns an interval value. The work
> of the
> function is to calculate the difference between the appointment
> timestamp and
> and the current timestamp, represented as an interval, and the the time
> required to travel from point A to B, represented as an interval.
>
> Appoint time: 01/14/2004 15:30
> Current time: 01/11/2004 10:43
> Appt Interval: @ 3 days 4 hours 17 mins
>
> Travel Time: 78 hours 17 minutes
> Travel Interval: @ 3 days 6 hours 17 mins
>
> As you can see, this truck is going to be 2 hours late. The return
> value I'm
> looking for is the difference between Appt. Interval and Travel
> Interval, as
> in: return (appt_interval - travel_interval). This value will be
> stored in a
> column of type interval. I would like for late values to be shown as
> negative. @ -2 hours. I thought that subtracting the larger interval
> from
> the small would return this but it is always the absolute value. The
> documentation states:
>
> interval values can be written with the following syntax:
>
> [(at)] quantity unit [quantity unit...] [direction]
>
> Where: quantity is a number (possibly signed);
>
> Considering the above statement I believed that I could show this
> difference
> as a negative value but I haven't been able to figure out how to do
> it. Can I
> do this, and if so, how?
>
> Thanks...
>
> Work: 1-336-372-6812
> Cell: 1-336-363-4719
> email: terry(at)esc1(dot)com
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 8: explain analyze is your friend
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2005-01-11 15:18:03 Re: Interval Question
Previous Message lol 2005-01-11 14:47:05 Re: PostgreSQL 8 on windows very slow