pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Owen Jacobson <ojacobson@osl.com>
To: pgsql-sql@postgresql.org
Subject: Re: Using a parameter in Interval
Date: Tue, 21 Mar 2006 16:57:52 -0800
Message-ID: <144D12D7DD4EC04F99241498BB4EEDCC25E93F@nelson.osl.com> (raw)

Here's one I used to convert an int to an interval in another project:

CREATE OR REPLACE FUNCTION to_interval (sec INTEGER) RETURNS INTERVAL AS $$
BEGIN
  RETURN (sec || ' seconds')::INTERVAL;
END;
$$ LANGUAGE 'plpgsql' IMMUTABLE STRICT;

select to_interval (5);
 to_interval
-------------
 00:00:05
(1 row)

You should be able to replace ' seconds' with ' weeks' just fine.

Excuse the outlook-ism,
-Owen
-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Davidson, Robert
Sent: Tuesday, March 21, 2006 4:53 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] Using a parameter in Interval


No matter how I try to concatenate, I can't seem to get a parameter to be used by INTERVAL in a function:
CREATE OR REPLACE FUNCTION Testing(TrailingWeeks int) RETURNS date AS $$
BEGIN
        RETURN current_date - INTERVAL (CAST(TrailingWeeks AS varchar) || ' weeks');
END;
$$ LANGUAGE plpgsql;
--select * from testing(1);
ERROR:  syntax error at or near "CAST" at character 34
QUERY:  SELECT  current_date - INTERVAL (CAST( $1  AS varchar) || ' weeks')
CONTEXT:  SQL statement in PL/PgSQL function "testing" near line 2
I have tried concatenating it as a declared variable (with and without apostrophes)
1 weeks
And 
'1 weeks'
With no success. Any tips?
Many thanks,
Robert



view thread (5+ messages)  latest in thread

Message-ID: <144D12D7DD4EC04F99241498BB4EEDCC25E93F@nelson.osl.com>
Permalink:  ../144D12D7DD4EC04F99241498BB4EEDCC25E93F@nelson.osl.com/
Also on:    postgresql.org/message-id/144D12D7DD4EC04F99241498BB4EEDCC25E93F@nelson.osl.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ojacobson@osl.com
  Subject: Re: Using a parameter in Interval
  In-Reply-To: <144D12D7DD4EC04F99241498BB4EEDCC25E93F@nelson.osl.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