time intervals
time intervals
I have two fields, timein & timeout, both defined as type "time". how can i get the difference between the two?? i am connecting to postgres through access97 and trying to run a simple query to show me timeout, timein, and timeout-timein, but the calculation is returning an error. thanks in advance! jt
Re: [SQL] time intervals
> I have two fields, timein & timeout, both defined as type "time". how can > i get the difference between the two?? i am connecting to postgres through > access97 and trying to run a simple query to show me timeout, timein, and > timeout-timein, but the calculation is returning an error. In SQL you can do a DATEDIFF...I'm not in front of postgresql so am not sure if it supports this. Syntax is select datediff (day, mydate, 'Jan 10 1999' from table If mydate = Jan 9, then the result is one. I think datediff accepts datetime, so if you specify it properly (i.e. '1 Jan 1999 10:05") then you'll get the right answers. Just change day to whatever you need. Regards, Mark. -- Mark Jewiss Knowledge Matters Limited
Re: [SQL] time intervals
JT Kirkpatrick wrote:I have two fields, timein & timeout, both defined as type "time". how can
i get the difference between the two?? i am connecting to postgres through
access97 and trying to run a simple query to show me timeout, timein, and
timeout-timein, but the calculation is returning an error.thanks in advance! jt
If your field is defined by tipe "time", the date is not important.In this case you can use the age(datetime, datetime) in this mode:
a. timein < timeout:
test=> select age(datetime('today'::date,'10:00:15'::time), datetime('today'::date,'12:25:24'::time));
age
----------------------------
@ 2 hours 25 mins 9 secs ago
(1 row)or
b. timein > timeout:
test=> select age(datetime('today'::date,'10:00:15'::time), datetime('today'::date,'2:25:24'::time));
age
-------------------------
@ 7 hours 34 mins 51 secs
(1 row)
I use PostgreSQL v6.5.0 (09-04-1999) on Red Hat LINUX 5.2 with 2.2.5 kernel.
--
Best,
George Moga,
george@flex.ro
Braila,ROMANIA
Re: [SQL] time intervals
Mark Jewiss ha scritto:> I have two fields, timein & timeout, both defined as type "time". how canDATEDIFF is not a SQL command maybe an Oracle or Sybase command.
> i get the difference between the two?? i am connecting to postgres through
> access97 and trying to run a simple query to show me timeout, timein, and
> timeout-timein, but the calculation is returning an error.In SQL you can do a DATEDIFF...I'm not in front of postgresql so am not
sure if it supports this.
You can create an operator for TIME - TIME...
Syntax isselect datediff (day, mydate, 'Jan 10 1999'
from tableIf mydate = Jan 9, then the result is one. I think datediff accepts
datetime, so if you specify it properly (i.e. '1 Jan 1999 10:05") then
you'll get the right answers. Just change day to whatever you need.Regards,
Mark.
--
Mark Jewiss
Knowledge Matters Limited-- Arithmetic operations that are permitted by SQL92:
-- -----------------------------------------
-- 1st operand|operator|2nd operand|result
-- -----------+--------+-----------+--------
-- datetime | - |datetime |interval
-- datetime | + |interval |datetime
-- datetime | - |interval |datetime
-- interval | + |datetime |datetime
-- interval | + |interval |interval
-- interval | - |interval |interval
-- interval | * |number |interval
-- interval | / |number |interval
-- number | * |interval |interval
-- -----------+--------+-----------+----------TIME - TIME = INTERVAL-------------------------------------------
create function time_mi_time(time,time) returns timespan as '
declare
i1 interval;
i2 interval;
begin
i1:= $1;
i2:= $2;
i1:=i1 - i2;
return i1;
end;' language 'plpgsql';create operator - (
leftarg=time,
rightarg=time,
procedure=time_mi_time,
commutator='-',
negator='+',
restrict=eqsel,
join=eqjoinsel
);hygea=> select time '12:30' - time '12:00';
?column?
--------
00:30:00
(1 row)--
______________________________________________________________
PostgreSQL 6.5.0 on i586-pc-linux-gnu, compiled by gcc 2.7.2.3
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jose'