Re: Best Fit SQL query statement - Mailing list pgsql-sql

From hubert depesz lubaczewski
Subject Re: Best Fit SQL query statement
Date
Msg-id 20070810075949.GA31829@depesz.com
Whole thread Raw
In response to Best Fit SQL query statement  (Kiran <kumar.m.kiran@gmail.com>)
Responses Re: Best Fit SQL query statement
List pgsql-sql
On Mon, Aug 06, 2007 at 01:57:07AM -0700, Kiran wrote:
> Could anyone  help me in writing Best Fit SQL statement.
> Suppose we have table t1 with coloumn t1 (text) with following rows.
> 98456
> 98457
> 9845
> 9846
> 984
> 985
> 98
> 99
> and if I query on 98456 the result must be 98456,
> However if I query on 98455 the result must be 9845
> and If I query 9849 the result must be 984

select t1.t1 from t1 where '98456' like t1.t1||'%' order by length(t1.t1) desc limit 1;

should be ok.

depesz

-- 
quicksil1er: "postgres is excellent, but like any DB it requires a
highly paid DBA.  here's my CV!" :)
http://www.depesz.com/ - blog dla ciebie (i moje CV)


pgsql-sql by date:

Previous
From: "Christian Kindler"
Date:
Subject: Re: Returnd Refcurser ( not fetchable
Next
From: hubert depesz lubaczewski
Date:
Subject: Re: Using function like where clause