Re: Best Fit SQL query statement

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Kiran <kumar(dot)m(dot)kiran(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Best Fit SQL query statement
Date: 2007-08-10 07:59:49
Message-ID: 20070810075949.GA31829@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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)

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message hubert depesz lubaczewski 2007-08-10 08:00:47 Re: [SQL] Using function like where clause
Previous Message Christian Kindler 2007-08-10 06:35:28 Re: Returnd Refcurser (<unnamed portal x> not fetchable