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 CDBE19DC828 for ; Tue, 21 Mar 2006 20:57:52 -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 02777-10 for ; Tue, 21 Mar 2006 20:57:55 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from nelson.osl.com (216-21-133-120.ip.van.radiant.net [216.21.133.120]) by postgresql.org (Postfix) with ESMTP id 994C19DC809 for ; Tue, 21 Mar 2006 20:57:50 -0400 (AST) Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0 Subject: Re: Using a parameter in Interval Date: Tue, 21 Mar 2006 16:57:52 -0800 Message-ID: <144D12D7DD4EC04F99241498BB4EEDCC25E93F@nelson.osl.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Using a parameter in Interval Thread-Index: AcZNSu6DV0gw/FIcQguhdVLpFtaIFAAAFMRg From: "Owen Jacobson" To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.128 required=5 tests=[AWL=0.128, UPPERCASE_25_50=0] X-Spam-Score: 0.128 X-Spam-Level: X-Archive-Number: 200603/341 X-Sequence-Number: 24687 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=20 '1 weeks' With no success. Any tips? Many thanks, Robert