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 A834852C2E for ; Mon, 25 Jul 2005 15:57:45 -0300 (ADT) 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 55270-07 for ; Mon, 25 Jul 2005 18:57:36 +0000 (GMT) Received: from smtp2.spawar.navy.mil (smtp2.spawar.navy.mil [128.49.4.3]) by svr1.postgresql.org (Postfix) with ESMTP id BC74852AD8 for ; Mon, 25 Jul 2005 15:57:35 -0300 (ADT) Received: from Seal11 (seal11.spawar.navy.mil [128.49.144.41]) by smtp2.spawar.navy.mil (8.12.11/8.12.11) with ESMTP id j6PIvDkA025514 for ; Mon, 25 Jul 2005 11:57:26 -0700 From: "Jonathan Lam" To: Subject: Re: Closes Match Date: Mon, 25 Jul 2005 11:57:11 -0700 Message-ID: <003601c5914a$b0582020$29903180@Seal11> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Importance: Normal In-Reply-To: <20050725174952.GA23263@wolff.to> X-SPAWAR-MailScanner: Found to be clean X-SPAWAR-MailScanner-SpamCheck: not spam (whitelisted), SpamAssassin (score=-2.6, required 5, autolearn=disabled, ALL_TRUSTED -2.82, AWL 0.22) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.575 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE X-Spam-Level: X-Archive-Number: 200507/225 X-Sequence-Number: 22203 unsubscribe -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Bruno Wolff III Sent: Monday, July 25, 2005 10:50 AM To: Christian Rusa Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] Closes Match On Mon, Jul 25, 2005 at 19:31:11 +0200, Christian Rusa wrote: > Hi there, > > I want to match a string against the database and get the closest > (shorter) match. > I was asking myself if its possible to realise this in SQL. > > So if the string is: > abcdefg > > And in the database are entries like: > a > ab > abc > > I just want to get back 'abc'. > > I looked around in the internet and the documentation but found no solution. You could ORDER BY the length of the strings descending and use LIMIT to return only one row. You can write a regular expression to do the matching. Something like: ^(a(b(c(d(e(fg?)?)?)?)?)?$ ---------------------------(end of broadcast)--------------------------- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq