Re: Substring

From: "Fernando Hevia" <fhevia(at)ip-tel(dot)com(dot)ar>
To: <raju(at)linux-delhi(dot)org>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Substring
Date: 2009-09-07 18:36:58
Message-ID: 9CC5D98DF7ED4FE7AFC70F08F75021E3@iptel.com.ar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> >
> > 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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message venkat 2009-09-07 19:53:21 Postgresql PostGIS installation on Widows Server 2003
Previous Message Raj Mathur 2009-09-05 04:27:40 Re: Substring