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 ED48F3CB03D for ; Fri, 17 Dec 2004 03:50:17 +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 37727-05 for ; Fri, 17 Dec 2004 03:50:00 +0000 (GMT) Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.250.190.2]) by svr1.postgresql.org (Postfix) with ESMTP id D75613CBA9F for ; Fri, 17 Dec 2004 03:50:03 +0000 (GMT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 8CE0C31959; Fri, 17 Dec 2004 04:50:03 +0100 (MET) From: Christopher Browne X-Newsgroups: pgsql.sql Subject: Re: plpgsql.. SELECT INTO ... WHERE FIELD LIKE Date: Thu, 16 Dec 2004 22:08:13 -0500 Organization: cbbrowne Computing Inc Lines: 36 Message-ID: References: <001901c4e3ca$7c71aca0$a101a8c0@home.axiontech.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@news.hub.org X-message-flag: Outlook is rather hackable, isn't it? X-Home-Page: http://www.cbbrowne.com/info/ X-Affero: http://svcs.affero.net/rm.php?r=cbbrowne User-Agent: Gnus/5.1007 (Gnus v5.10.7) XEmacs/21.4 (Security Through Obscurity, linux) Cancel-Lock: sha1:7eG3WW9ToM8lTi8QdJbTR6PH8aY= To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.437 tagged_above=0 required=5 tests=AWL, INFO_TLD, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200412/111 X-Sequence-Number: 19731 Oops! yudie@axiontech.com ("Yudie") was seen spray-painting on a wall: > 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%' > > CREATE OR REPLACE FUNCTION custlike(text) RETURNS INT4 AS' >   DECLARE >     keyword ALIAS FOR $1; >     RS RECORD; >   BEGIN >     SELECT INTO RS id FROM customer WHERE firstname like keyword% LIMIT 1; >     IF FOUND THEN >       RETURN RS.id; >     ELSE >        RETURN NULL; >     END IF; >  END' > LANGUAGE 'PLPGSQL'; Try: SELECT INTO RS ID FROM CUSTOMER WHERE FIRSTNAME LIKE KEYWORD || ''%'' LIMIT 1; You append KEYWORD and a '%' together using ||. You need to use doubled quotes inside the quoted environment; one gets stripped off so that the stored procedure will wind up containing the query: SELECT INTO RS ID FROM CUSTOMER WHERE FIRSTNAME LIKE KEYWORD || '%' LIMIT 1; -- let name="cbbrowne" and tld="gmail.com" in name ^ "@" ^ tld;; http://linuxfinances.info/info/sgml.html C is almost a real language. (see assembler) Even the name sounds like it's gone through an optimizing compiler. Get rid of all of those stupid brackets and we'll talk. (see LISP)