pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kristis Makris <kristis.makris@datasoft.com>
To: pgsql-sql@postgresql.org
Subject: Using the extract() function in plpgsql
Date: 20 Jun 2001 09:49:43 -0700
Message-ID: <200106201649.f5KGnoa95364@postgresql.org> (raw)

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




view thread (11+ messages)  latest in thread

Message-ID: <200106201649.f5KGnoa95364@postgresql.org>
Permalink:  ../200106201649.f5KGnoa95364@postgresql.org/
Also on:    postgresql.org/message-id/200106201649.f5KGnoa95364@postgresql.org

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: kristis.makris@datasoft.com
  Subject: Re: Using the extract() function in plpgsql
  In-Reply-To: <200106201649.f5KGnoa95364@postgresql.org>

* 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