Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 match maximum character match

Author  Topic 

karthik_192me
Starting Member

3 Posts

Posted - 2011-05-17 : 12:23:37
Hi,

I am new to this forum,new bee to SQL.,my DB is
digit,name
93,RAM
9379,GURU
937,RAVI

want to get maximum character match,like when i give 93799321205
i shd get maximum match, output shd be 9379,guru,
tried some how below but not getting exact output.,pl help
SELECT name,digit from MDL where LEFT('93799321205',length(digit))=digit;

edit: moved to proper forum

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-17 : 13:55:25
Would this work for you? I am assuming that the digit column is of type varchar. If not you will need to cast it as varchar

SELECT
*
FROM theTable a
WHERE NOT EXISTS
(
SELECT * FROM theTable b WHERE b.digit LIKE a.digit+'%' AND a.digit <> b.digit
)

Go to Top of Page

karthik_192me
Starting Member

3 Posts

Posted - 2011-05-19 : 12:12:03
Hi

Thanks u very much for ur reply, i finally got the solution.,
digit data type is text.


SELECT digit from MDL where LEFT('9940061716',length(digit))=digit order by length(digit) desc limit 1;
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-05-30 : 07:20:00
Note that this forum is for MS SQL Server. For mysql questions try at www.mysql.com

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -