Using the extract() function in plpgsql

From: Kristis Makris <kristis(dot)makris(at)datasoft(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Using the extract() function in plpgsql
Date: 2001-06-20 16:49:43
Message-ID: 200106201649.f5KGnoa95364@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-sql

Hello all,

I'm using PostgreSQL 7.1.2 on i686-pc-linux-gnu, compiled by GCC
egcs-2.91.66

I'm having trouble making use of the extract function in plpgsql. The
examples in the documentation show that the function can be called in
the following way:

SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
Result: 442800

If the function is called in the above way through the psql client, then
a result is retrieved. But how can the extract() be used in a plpgsql
function without using the EXECUTE statement? Here's what I've been
trying:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CREATE FUNCTION ACFTProduced(TIMESTAMP, TIMESTAMP, DECIMAL(9,1)) RETURNS
DECIMAL(9,2) AS '
DECLARE
lFromDate ALIAS FOR $1;
lToDate ALIAS FOR $2;
lFlow ALIAS FOR $3;
lACFTProduced NUMERIC;
lDateInterval INTERVAL;
lSeconds INT4;
lExecStmt TEXT;
BEGIN
lDateInterval = lToDate - lFromDate;

lExecStmt = ''SELECT extract(epoch from interval '' ||
'''''''' || lDateInterval || '''''''' ||
'') INTO lSeconds'';

--
--I would like to execute the following statement
--
--SELECT extract(epoch from interval lDateInterval)
--INTO lDays;
--

RAISE NOTICE ''The difference interval is: %'', lDateInterval;
RAISE NOTICE ''The seconds are: %'', lSeconds;
RAISE NOTICE ''The execStmt is: %'', lExecStmt;

EXECUTE lExecStmt;

RETURN lACFTProduced;
END;
' LANGUAGE 'plpgsql';
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

If the function is called:

test=# select acftproduced('2001-04-18 16:55:11-07', current_timestamp,
4.5);
NOTICE: The difference interval is: 62 days 16:39:07
NOTICE: The seconds are: <NULL>
NOTICE: The execStmt is: SELECT extract(epoch from interval '62 days
16:39:07') INTO lSeconds
ERROR: EXECUTE of SELECT ... INTO is not implemented yet

Is there any other way the extract() can be called?

Thanks for any help
-Kristis

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Ing. Roberto Andrade Fonseca 2001-06-20 17:29:40 List of DDL commands?
Previous Message James Moore 2001-06-20 16:46:09 Problems upgrading from 7.1beta4 to 7.1.0

Browse pgsql-sql by date

  From Date Subject
Next Message Frank Bax 2001-06-20 17:13:00 Re: Help on a sql querry.
Previous Message Hunter, Ray 2001-06-20 16:45:20 Functions and Triggers