Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id BACE5632DFB for ; Sat, 5 Sep 2009 01:11:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 18607-06 for ; Sat, 5 Sep 2009 04:10:52 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from tomts13-srv.bellnexxia.net (tomts13-srv.bellnexxia.net [209.226.175.34]) by mail.postgresql.org (Postfix) with ESMTP id 9FD436336CE for ; Sat, 5 Sep 2009 01:10:52 -0300 (ADT) Received: from toip7.srvr.bell.ca ([209.226.175.124]) by tomts13-srv.bellnexxia.net (InterMail vM.5.01.06.13 201-253-122-130-113-20050324) with ESMTP id <20090905041051.ZWRI27263.tomts13-srv.bellnexxia.net@toip7.srvr.bell.ca> for ; Sat, 5 Sep 2009 00:10:51 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: As4FAKd+oUpA53n0/2dsb2JhbACBU44xyV+EFgU Received: from bas4-london14-1088911860.dsl.bell.ca (HELO t60p.bax.ca) ([64.231.121.244]) by toip7.srvr.bell.ca with ESMTP; 05 Sep 2009 00:13:58 -0400 Message-ID: <4AA1E4C9.1000108@sympatico.ca> Date: Sat, 05 Sep 2009 00:10:49 -0400 From: Frank Bax User-Agent: Thunderbird 2.0.0.19 (X11/20090301) MIME-Version: 1.0 CC: pgsql-sql@postgresql.org Subject: Re: Substring References: <15318.85292.qm@web53905.mail.re2.yahoo.com> <200909050802.10371.raju@linux-delhi.org> In-Reply-To: <200909050802.10371.raju@linux-delhi.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.307 tagged_above=-10 required=5 tests=BAYES_00=-2.599, MISSING_HEADERS=1.292 X-Spam-Level: X-Archive-Number: 200909/29 X-Sequence-Number: 33094 Raj Mathur wrote: > On Saturday 05 Sep 2009, bilal ghayyad wrote: >> I have an sql script function that take one text parameter "funct >> (text)", what I need to do is the following: >> >> If the parameter name is string and its value was for example "abcd" >> then I need to do a query based on ab and then based on the abc, how? >> >> Example: >> >> SELECT * from voipdb where prefix like string >> >> But I need the string to be ab and then to be abc? How I can assign >> the string to the first character and then to the first and second? >> In other words, how can I can take part of the string to do query on >> it? > > From your example the following brute-force method should work (not > tested): > > select * from voipdb where prefix like substring(string from 1 for 2) || > '%' or prefix like substring(string from 1 for 3) || '%'; > > However, I don't understand why you'd want to search for both 'ab' and > 'abc' in the same query, since the first condition is a superset of the > second one. Given that tablename is "voipdb"; I wonder if OP really wants to write a query that finds the row where argument to function matches the most number of leading characters in "prefix". If voipdb table contains: ab, abc, def, defg; then calling function with "abc" or "abcd" returns "abc" and calling function with "defh" returns "def". If this is the real problem to be solved; then brute force is one solution; but I'm left wondering if a single query might return desired result (a single row).