Re: Substring - Mailing list pgsql-sql

From Fernando Hevia
Subject Re: Substring
Date
Msg-id 9CC5D98DF7ED4FE7AFC70F08F75021E3@iptel.com.ar
Whole thread Raw
In response to Re: Substring  (Raj Mathur <raju@linux-delhi.org>)
List 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.



pgsql-sql by date:

Previous
From: Raj Mathur
Date:
Subject: Re: Substring
Next
From: venkat
Date:
Subject: Postgresql PostGIS installation on Widows Server 2003