dbTalk Databases Forums  

INTERVAL in a function

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss INTERVAL in a function in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ron St-Pierre
 
Posts: n/a

Default INTERVAL in a function - 11-08-2004 , 06:15 PM






I have a simple function which I use to set up a users' expiry date. If
a field in a table contains an interval then
this function returns a timestamp some time in the future (usually two
weeks), null otherwise. I can't pass the
interval from the table into a variable properly within the function.
Any ideas?

CREATE OR REPLACE FUNCTION getUnitTimeLength(int) RETURNS timestamp AS '

DECLARE
grpID ALIAS FOR $1;
intval INTERVAL;
exptime TIMESTAMP;
BEGIN
SELECT INTO intval unitTimeLength::INTERVAL FROM customer.groups WHERE groupsID = grpID;
IF intval IS NULL THEN
RETURN NULL;
ELSE
SELECT INTO exptime current_timestamp + INTERVAL ''intval'';
RETURN exptime;
END IF;
END;
' LANGUAGE 'plpgsql';


SELECT getUnitTimeLength(55);

ERROR: invalid input syntax for type interval: "intval"
CONTEXT: PL/pgSQL function "getunittimelength" line 11 at select into variables


However if I change the else clause to this:
ELSE
SELECT INTO exptime current_timestamp;
RETURN exptime;
END IF;
it works:
----------------------------
2004-11-08 16:14:40.273597
(1 row)


Thanks
Ron








---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Reply With Quote
  #2  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: INTERVAL in a function - 11-08-2004 , 07:00 PM






On Mon, Nov 08, 2004 at 04:15:34PM -0800, Ron St-Pierre wrote:

Quote:
SELECT INTO exptime current_timestamp + INTERVAL ''intval'';
You're using the literal value 'intval' instead of its value, thus
the syntax error. You can simplify the statement to this:

exptime := current_timestamp + intval;

But I think the entire function can be shortened to:

CREATE OR REPLACE FUNCTION getUnitTimeLength(int) RETURNS TIMESTAMP AS '
SELECT CURRENT_TIMESTAMP::timestamp + unitTimeLength
FROM customer.groups
WHERE groupsID = $1
' LANGUAGE sql;

You don't need to check for NULL because the result of the addition
will already be NULL if either operand is NULL. Casting CURRENT_TIMESTAMP
is necessary to avoid a "return type mismatch" error.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



Reply With Quote
  #3  
Old   
Ron St-Pierre
 
Posts: n/a

Default Re: INTERVAL in a function - 11-09-2004 , 11:07 AM



Michael Fuhr wrote:

Quote:
On Mon, Nov 08, 2004 at 04:15:34PM -0800, Ron St-Pierre wrote:



SELECT INTO exptime current_timestamp + INTERVAL ''intval'';



You're using the literal value 'intval' instead of its value, thus
the syntax error.

Of course, I should have caught that.

Quote:
You can simplify the statement to this:

exptime := current_timestamp + intval;

But I think the entire function can be shortened to:

CREATE OR REPLACE FUNCTION getUnitTimeLength(int) RETURNS TIMESTAMP AS '
SELECT CURRENT_TIMESTAMP::timestamp + unitTimeLength
FROM customer.groups
WHERE groupsID = $1
' LANGUAGE sql;

You don't need to check for NULL because the result of the addition
will already be NULL if either operand is NULL. Casting CURRENT_TIMESTAMP
is necessary to avoid a "return type mismatch" error.



Perfect.
Thanks Michael!


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.