pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stijn Vanroye <s.vanroye@Farcourier.com>
To: pgsql-sql@postgresql.org
Subject: Difference between two times as a numeric value in a stored procedure.
Date: Fri, 4 Jun 2004 10:11:57 +0200
Message-ID: <736CEAA26E7E3F48943458F760E7A32603B621@fs1010.farcourier.com> (raw)

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 06: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 = time \'00:00\' THEN
     enddate := (current_date+1)+endtime;
  ELSE
      enddate := current_date+endtime;
  END IF;
  IF begintime < time \'06:00\' THEN
    begindate := current_date + time \'06:00\';
  ELSE
    begindate := current_date+begintime;
  END IF;
  tmpresult := 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 example, in stead of 4:30 i would like 4.5 as a result. I have searched the documentation but could not find any way to substract time/timestamp values and get a numeric/float as a result. When I try to CAST the interval to a numeric or float value I get an error (cannot cast time without tz to ...). Same goes for trying to cast the beginvalues and then substract them. Does anyone have any idea how I can solve/circumvent this problem? Is there a function 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, getdayhours(begintime,endtime), getnighthours(begintime,endtime) FROM workhour


Thanks in advance.

Stijn Vanroye



view thread (4+ messages)  latest in thread

Message-ID: <736CEAA26E7E3F48943458F760E7A32603B621@fs1010.farcourier.com>
Permalink:  ../736CEAA26E7E3F48943458F760E7A32603B621@fs1010.farcourier.com/
Also on:    postgresql.org/message-id/736CEAA26E7E3F48943458F760E7A32603B621@fs1010.farcourier.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: s.vanroye@Farcourier.com
  Subject: Re: Difference between two times as a numeric value in a stored procedure.
  In-Reply-To: <736CEAA26E7E3F48943458F760E7A32603B621@fs1010.farcourier.com>
  References: <736CEAA26E7E3F48943458F760E7A32603B621@fs1010.farcourier.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox