From: | Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> |
---|---|
To: | litu16 <litumelendez(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: Get the difference between two timestamp cells but in a special format in PostgreSQL |
Date: | 2015-06-25 22:27:11 |
Message-ID: | 558C803F.5080404@aklaver.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 06/24/2015 09:11 PM, litu16 wrote:
> This is my table...
>
> <http://postgresql.nabble.com/file/n5855074/screenshot.jpg>
>
> I would like to get the time difference between 'time_type = Lap' AND
> 'time_type = Start' in order to fill 'time_elapse'. Im almost sure that this
> code works...
>
> * CREATE OR REPLACE FUNCTION timediff()
> RETURNS trigger AS
> $BODY$
> DECLARE
> t_ix real;
>
> BEGIN
> IF NEW.time_type = 'Lap' THEN
> SELECT t.time FROM table_ebscb_spa_log04 t WHERE t.fn_name =
> NEW.fn_name AND t.time_type = 'Start' ORDER BY t.stmtserial DESC LIMIT 1
> INTO t_ix;
> IF NOT FOUND THEN
> RAISE EXCEPTION USING MESSAGE = 'There is not any previous
> row...';
> ELSE
> NEW.time_elapse := t_ix - NEW.time;
> END IF;
> END IF;
> return NEW;
> END
> $BODY$
> LANGUAGE plpgsql VOLATILE*
>
> But I don't know how to get the time difference between the two timestamps
> cells in a special format: (years/months/days
> hours:minutes:seconds:miliseconds) like this...
Well subtracting two timestamps gets you an interval:
http://www.postgresql.org/docs/9.4/interactive/functions-datetime.html
Table 9-27. Date/Time Operators
which you can convert:
http://www.postgresql.org/docs/9.4/interactive/functions-formatting.html
to_char(interval, text) text convert interval to string
to_char(interval '15h 2m 12s', 'HH24:MI:SS')
so:
postgres(at)production=# SELECT to_char(interval '15h 2m 12s', 'HH24:MI:SS');
to_char
----------
15:02:12
(1 row)
>
> * 0y/0m/0d 00:00:01.001*
>
> Is this possible??
> Thanks Advanced.
>
>
>
> --
> View this message in context: http://postgresql.nabble.com/Get-the-difference-between-two-timestamp-cells-but-in-a-special-format-in-PostgreSQL-tp5855074.html
> Sent from the PostgreSQL - general mailing list archive at Nabble.com.
>
>
--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com
From | Date | Subject | |
---|---|---|---|
Next Message | Gavin Flower | 2015-06-25 23:25:04 | Re: Re: INSERT a real number in a column based on other columns OLD INSERTs |
Previous Message | Colin Lieberman | 2015-06-25 22:23:01 | Re: Get the difference between two timestamp cells but in a special format in PostgreSQL |