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.
| 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:1231234123451236432100And 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 |
|
|
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? |
 |
|
|
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:1231234123451236432100And 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 likeDECLARE @Val varchar(100)SET @Val = '1234987'SELECT TOP 1 ValFROM(SELECT 123 AS Val UNION ALLSELECT 1234 UNION ALLSELECT 12345 UNION ALLSELECT 12364 UNION ALLSELECT 32100)tWHERE @Val LIKE '%' + CAST(Val as varchar(10)) + '%'ORDER BY LEN(Val) DESCoutput-----------------------Val1234 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 resultsSELECT TOP 1 ValFROM yourtableWHERE @Val LIKE '%' + CAST(Val as varchar(10)) + '%'ORDER BY LEN(Val) DESC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|