Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 41FBD634F41 for ; Mon, 7 Sep 2009 15:38:20 -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 02599-01-8 for ; Mon, 7 Sep 2009 18:38:06 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from pyxis.powervt.com.ar (host6.powervt.com.ar [190.11.152.66]) by mail.postgresql.org (Postfix) with ESMTP id E6A69634E51 for ; Mon, 7 Sep 2009 15:35:55 -0300 (ADT) Received: from pcfhevia ([201.216.249.98]) by pyxis.powervt.com.ar (IPGroup Mail Server) with ASMTP id 1OT48953; Mon, 07 Sep 2009 15:35:49 -0300 From: "Fernando Hevia" To: , References: <15318.85292.qm@web53905.mail.re2.yahoo.com> <200909050802.10371.raju@linux-delhi.org> <4AA1E4C9.1000108@sympatico.ca> <200909050957.40591.raju@linux-delhi.org> Subject: Re: Substring Date: Mon, 7 Sep 2009 15:36:58 -0300 Organization: IP-TEL Message-ID: <9CC5D98DF7ED4FE7AFC70F08F75021E3@iptel.com.ar> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <200909050957.40591.raju@linux-delhi.org> Thread-Index: Acot4VKXpYCRr8zoRbiOYsb1iWlpcwCB648Q X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.304 tagged_above=-10 required=5 tests=AWL=0.303, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200909/31 X-Sequence-Number: 33096 > > > > 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). > > Something like this may help in that case (note, we're > completely in the realm of creating imaginary problems and > solving them now :) > > select * from voipdb where prefix <= string order by prefix > desc limit 1; > > Regards, > > -- Raju Hum, I wonder if some kind of best-matching query is what you are looking for: SELECT * FROM voipdb WHERE prefix IN ( SELECT substr(string, 1, i) FROM generate_series(1, length(string)) i ); Cheers, Fernando.