如何在SQL Server中查找与给定字符串相似的字符串? [英] How to find strings which are similar to given string in SQL server?
问题描述
我有一个包含几个字符串列的SQL Server表.我需要编写一个获取字符串并在SQL Server表中搜索相似字符串的应用程序.
I have a SQL server table which contains several string columns. I need to write an application which gets a string and search for similar strings in SQL server table.
例如,如果我将مختار"或مختر"作为输入字符串,则应从SQL表中获取它们:
For example, if I give the "مختار" or "مختر" as input string, I should get these from SQL table:
1 - مختاری
2 - شهاب مختاری
3 - شهاب الدین مختاری
我已经在网上搜索了解决方案,但没有发现任何有用的信息.我已经阅读了这个问题,但这对我没有帮助,因为:
I've searched the net for a solution but I have found nothing useful. I've read this question , but this will not help me because:
- 我使用的是MS SQL Server,而不是MySQL
- 我的表内容是波斯语,所以我不能使用Levenshtein距离和类似的方法
- 我更喜欢仅使用SQL Server的解决方案,而不是基于索引或基于守护程序的解决方案.
最好的解决方案是可以帮助我们按相似度对结果进行排序的解决方案,但这是可选的.
The best solution would be a solution which help us sort result by similarity, but, its optional.
您对此有何建议?
谢谢
推荐答案
嗯.类似"?
--This part searches for a string you want
declare @MyString varchar(max)
set @MyString = (Select column from table
where **LOGIC TO FIND THE STRING GOES HERE**)
--This part searches for that string
select searchColumn, ABS(Len(searchColumn) - Len(@MyString)) as Similarity
from table where data LIKE '%' + @MyString + '%'
Order by Similarity, searchColumn
相似性部分类似于您发布的内容.如果字符串更相似"意味着它们具有相似的长度,则它们在结果查询中将更高. 绝对的部分可以避免,但是我还是以防万一.
The similarity part is something like the thing you posted. If the strings are "more similar" meaning that they have a similar length, they will be higher on the results query. The absolute part can be avoided obviously but I did it just in case.
希望有帮助=-)
这篇关于如何在SQL Server中查找与给定字符串相似的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!