X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 2A44E9DC809 for ; Tue, 21 Mar 2006 21:03:01 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 07159-03 for ; Tue, 21 Mar 2006 21:03:04 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 37CB09DC828 for ; Tue, 21 Mar 2006 21:02:59 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id B0F753524D; Tue, 21 Mar 2006 17:03:02 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id AEDFF35211; Tue, 21 Mar 2006 17:03:02 -0800 (PST) Date: Tue, 21 Mar 2006 17:03:02 -0800 (PST) From: Stephan Szabo To: "Davidson, Robert" Cc: pgsql-sql@postgresql.org Subject: Re: Using a parameter in Interval In-Reply-To: <8333C841129E074E9F83FC80676BA76E0BEBEF@exchg-sea3-03.ant.amazon.com> Message-ID: <20060321165750.J85509@megazone.bigpanda.com> References: <8333C841129E074E9F83FC80676BA76E0BEBEF@exchg-sea3-03.ant.amazon.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.14 required=5 tests=[AWL=0.140] X-Spam-Score: 0.14 X-Spam-Level: X-Archive-Number: 200603/342 X-Sequence-Number: 24688 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.