X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 72769D1B256 for ; Fri, 4 Jun 2004 05:15:09 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 65564-07 for ; Fri, 4 Jun 2004 08:15:10 +0000 (GMT) Received: from mail.cobweb.nl (mx1.cobweb.nl [80.242.96.4]) by svr1.postgresql.org (Postfix) with ESMTP id 7ABECD1B1F8 for ; Fri, 4 Jun 2004 05:15:06 -0300 (ADT) Received: from farcourier.com (dp3-26.dsl.cobweb.nl [193.67.120.154] (may be forged)) by mail.cobweb.nl (8.12.8/8.12.7) with ESMTP id i548FBxK056459 for ; Fri, 4 Jun 2004 10:15:12 +0200 (CEST) (envelope-from s.vanroye@Farcourier.com) Subject: Difference between two times as a numeric value in a stored procedure. MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Date: Fri, 4 Jun 2004 10:11:57 +0200 Content-Class: urn:content-classes:message X-MimeOLE: Produced By Microsoft Exchange V6.0.5762.3 Message-ID: <736CEAA26E7E3F48943458F760E7A32603B621@fs1010.farcourier.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Difference between two times as a numeric value in a stored procedure. thread-index: AcRKC6FX5T0vVsvcTbaMmLp7nIHZ6A== From: "Stijn Vanroye" To: X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200406/29 X-Sequence-Number: 17974 Hello List, I am writing two stored procedure which alternatively returns the dayhours = and nighthours of two times. (nighthours are considered between 00:00 and 0= 6:00). As an example here is the getdayhours function: ---------------------------------------------------------------------------= --------------------- CREATE OR REPLACE FUNCTION public.getdayhours(time, time) RETURNS interval AS 'DECLARE begintime ALIAS FOR $1; endtime ALIAS FOR $2; begindate timestamp; enddate timestamp; tmpresult interval; BEGIN IF endtime =3D time \'00:00\' THEN enddate :=3D (current_date+1)+endtime; ELSE enddate :=3D current_date+endtime; END IF; IF begintime < time \'06:00\' THEN begindate :=3D current_date + time \'06:00\'; ELSE begindate :=3D current_date+begintime; END IF; tmpresult :=3D enddate-begindate; IF tmpresult<\'00:00\' THEN return \'00:00\'; ELSE return tmpresult; END IF; END;' LANGUAGE 'plpgsql' VOLATILE; ---------------------------------------------------------------------------= --------------------- The working of the functions is not the problem, but the return type is. I = can't seem to find a way to substract two time values (or timestamp values)= and get a numeric/float value. I always get the INTERVAL datatype. For exa= mple, in stead of 4:30 i would like 4.5 as a result. I have searched the do= cumentation but could not find any way to substract time/timestamp values a= nd get a numeric/float as a result. When I try to CAST the interval to a nu= meric or float value I get an error (cannot cast time without tz to ...). S= ame goes for trying to cast the beginvalues and then substract them. Does a= nyone have any idea how I can solve/circumvent this problem? Is there a fun= ction I can use? I don't know if it helps but I'm going to use the functions like this: SELECT workhour_id, employee_id, task_id, whdate, begintime, endtime, getda= yhours(begintime,endtime), getnighthours(begintime,endtime) FROM workhour Thanks in advance. Stijn Vanroye