Sql查询到单词的完全匹配

时间:2013-08-08 04:36:16

标签: sql sql-server sql-server-2008

我的查询就像这样

declare @searchWord varchar(100)
set @searchWord = 'carb9' 
declare @tableWord varchar(100)
set @tableWord = 'Racing Carb9 uretor'
--set @tableWord =  'carb9'

select 1 
where  @tableWord like '%[^a-z0-9]' + @searchWord + '[^a-z0-9]%'
or @tableWord like @searchWord + '[^a-z0-9]%'
or @tableWord like '%[^a-z0-9]' + @searchWord 

当@tableWord是'Racing Carb9 uretor'时,此查询有效但@tableword只是'carb9'时不起作用。它应该在两种情况下都有效。我做错了什么?

2 个答案:

答案 0 :(得分:2)

您可以在查询中添加条件@tableWord = @searchWord。目前,没有任何条件可以在这种情况下恢复原状

declare @searchWord varchar(100)
set @searchWord = 'carb9' 
declare @tableWord varchar(100)
set @tableWord = 'Racing Carb9 uretor'
--set @tableWord =  'carb9'

select 1 
where  @tableWord like '%[^a-z0-9]' + @searchWord + '[^a-z0-9]%'
or @tableWord like @searchWord + '[^a-z0-9]%'
or @tableWord like '%[^a-z0-9]' + @searchWord 
or @tableWord = @searchWord 

答案 1 :(得分:0)

@ tableWord ='carb9'将不匹配where子句的任何模式,