X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9AF193B6675 for ; Fri, 17 Dec 2004 00:46:42 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 75696-06 for ; Fri, 17 Dec 2004 00:46:24 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id F25DC3CB6EE for ; Fri, 17 Dec 2004 00:46:20 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id iBH0kHvC013258 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 16 Dec 2004 17:46:20 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id iBH0kHTO009147; Thu, 16 Dec 2004 17:46:17 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id iBH0kHnU009146; Thu, 16 Dec 2004 17:46:17 -0700 (MST) (envelope-from mfuhr) Date: Thu, 16 Dec 2004 17:46:17 -0700 From: Michael Fuhr To: Yudie Cc: pgsql-sql@postgresql.org Subject: Re: plpgsql.. SELECT INTO ... WHERE FIELD LIKE Message-ID: <20041217004617.GA9115@winnie.fuhr.org> References: <001901c4e3ca$7c71aca0$a101a8c0@home.axiontech.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <001901c4e3ca$7c71aca0$a101a8c0@home.axiontech.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.106 tagged_above=0 required=5 tests=AWL, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200412/109 X-Sequence-Number: 19729 On Thu, Dec 16, 2004 at 05:53:43PM -0600, Yudie wrote: > How in plpgsql use LIKE with a variable? > > let say I want to do this query: > > SELECT INTO RS id FROM customer WHERE firstname LIKE keyword% LIMIT 1; > > keyword is a variable, in this case I want to find name like 'Jo%' Use the concatenation operator (||): SELECT ... WHERE firstname LIKE keyword || ''%'' LIMIT 1; Notice the two single quotes, which are necessary if the function body is surrounded by quotes. Life gets easier in 8.0 with dollar quoting. Are you planning to add more code to your PL/pgSQL function? If not, then you could replace it with a simple SQL function: CREATE OR REPLACE FUNCTION custlike(TEXT) RETURNS INTEGER AS ' SELECT id FROM customer WHERE firstname LIKE $1 || ''%'' LIMIT 1; ' LANGUAGE sql; The function will return NULL if it finds no records. -- Michael Fuhr http://www.fuhr.org/~mfuhr/