Re: Closes Match

From: "Jonathan Lam" <lamj(at)spawar(dot)navy(dot)mil>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Closes Match
Date: 2005-07-25 18:57:11
Message-ID: 003601c5914a$b0582020$29903180@Seal11
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

unsubscribe
-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org] On Behalf Of Bruno Wolff III
Sent: Monday, July 25, 2005 10:50 AM
To: Christian Rusa
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Closes Match

On Mon, Jul 25, 2005 at 19:31:11 +0200,
Christian Rusa <christian(at)rusa(dot)at> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Eugene E. 2005-07-26 07:41:02 REINDEX DATABASE
Previous Message Bruno Wolff III 2005-07-25 17:49:52 Re: Closes Match