我会尝试用@name
替换您的'% %'
中的空格
像
SET @nameFilter = REPLACE(@name,' ','% %')
SELECT name FROM customers WHERE name like '%'+ @ nameFilter + '%'
,
全文搜索似乎是最好的方法。但是您可以通过拆分搜索词并查找每个单词来在单词级别上对此进行近似:
with words as (
select value as word
from string_split(@name)
)
select c.name
from customers c cross apply
(select count(*) as cnt
from words w
where c.name like '%' + c.word + '%'
) w
where w.cnt = (select count(*) from words);
这使用了SQL Server的最新版本中提供的string_split()
功能。该功能有在线版本,可用于较早的版本。
,
在我可以发布之前,已经回答/接受了这个问题,发布的@ sugar2Code是我将如何做的。
也就是说,我不清楚您是否希望名字 和 都需要相似或只是其中之一。我汇总的内容将使您可以决定使用参数。
CustomerName FNMatch LNMatch
------------------------------ ----------- -----------
Johny Smith 1 1
Mr. Smithers 0 1
Johnathan Smithe 1 1
当@partialmatch = 1时,您得到:
[ResponseCache (NoStore = true,Location = ResponseCacheLocation.None)]
将@partialMatch设置为0将排除“史密瑟斯先生”。
本文链接:https://www.f2er.com/3149973.html