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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select Closest Match?

Author  Topic 

rhart3
Starting Member

2 Posts

Posted - 2010-04-01 : 21:43:47
Hey,

I'm hoping someone can help me get rid of this headache I've had for the last couple days. I need to find the closest match to a variable in a select statement. The values being queried are numbers, not words, however I don't want the closest numeric match. I want the closest character match(es). If that doesn't make sense...

Let's say I have a column of data:

123
1234
12345
12364
32100

And I have a stored procedure that simply runs:

select * from table where column like '@variable%';

This works correctly if the variable entered is "1234" because the results that come back are 1234 and 12345. My problem is when the user enters the variable as "1234987"

My goal is to have the returned result in this case be 1234. Basically, I want to match the variable as far as possible starting from the first digit, and return whatever the closest match is that it can find.

Can anyone point me in the right direction?

Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-01 : 22:13:20
It sounds like you need to convert it to a varchar field and turn on full-text. You can then use CONTAINS.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rhart3
Starting Member

2 Posts

Posted - 2010-04-01 : 22:48:47
Maybe I'm misunderstanding CONTAINS, but doesn't that function similar to LIKE?

Based on what I'm reading on http://msdn.microsoft.com/en-us/library/ms187787.aspx, in my example there is nothing in the table that contains "1234987" so wouldn't CONTAINS return no results?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-02 : 02:18:41
quote:
Originally posted by rhart3

Hey,

I'm hoping someone can help me get rid of this headache I've had for the last couple days. I need to find the closest match to a variable in a select statement. The values being queried are numbers, not words, however I don't want the closest numeric match. I want the closest character match(es). If that doesn't make sense...

Let's say I have a column of data:

123
1234
12345
12364
32100

And I have a stored procedure that simply runs:

select * from table where column like '@variable%';

This works correctly if the variable entered is "1234" because the results that come back are 1234 and 12345. My problem is when the user enters the variable as "1234987"

My goal is to have the returned result in this case be 1234. Basically, I want to match the variable as far as possible starting from the first digit, and return whatever the closest match is that it can find.

Can anyone point me in the right direction?

Thanks in advance.



some thing like

DECLARE @Val varchar(100)
SET @Val = '1234987'

SELECT TOP 1 Val
FROM
(

SELECT 123 AS Val UNION ALL
SELECT 1234 UNION ALL
SELECT 12345 UNION ALL
SELECT 12364 UNION ALL
SELECT 32100
)t
WHERE @Val LIKE '%' + CAST(Val as varchar(10)) + '%'
ORDER BY LEN(Val) DESC

output
-----------------------
Val
1234



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-02 : 02:22:35
replace the subquery t with your actual table in above query and you'll get results

SELECT TOP 1 Val
FROM yourtable
WHERE @Val LIKE '%' + CAST(Val as varchar(10)) + '%'
ORDER BY LEN(Val) DESC


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -