Re: Using a parameter in Interval

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: "Davidson, Robert" <robdavid(at)amazon(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Using a parameter in Interval
Date: 2006-03-22 01:03:02
Message-ID: 20060321165750.J85509@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, 21 Mar 2006, Davidson, Robert wrote:

> 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?

You'd need a cast, not INTERVAL foo as the latter is for interval literals
(and CAST... is not a valid interval literal even if the output of the
concatenation looks like an interval literal). I'd go with the suggestion
of using int * interval instead of concatenation in any case.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruno Wolff III 2006-03-22 03:29:24 Re: have you feel anything when you read this ?
Previous Message Owen Jacobson 2006-03-22 00:57:52 Re: Using a parameter in Interval