我想创建一个查询,它可以在我的"条件"中使用短语的单词。 (像谷歌一样)
实施例: 我的短语:' Foo Bar Test'
我的数据库表:
|柱|
'嗨Bar'
'测试栏'
' Hi Foo'
'这里没什么'
' Foo再次'
我等待的结果:
|柱|
'嗨Bar'
'测试栏'
' Hi Foo'
' Foo Again'
答案 0 :(得分:1)
您可以在Sql server中使用FULL Text Index
执行此操作。也可以使用split string函数实现
Declare @str varchar(500) = 'Foo Bar Test'
SELECT distinct tc.*
FROM (VALUES ('Hi Bar' ),
('Test Bar' ),
('Hi Foo' ),
('Nothing here' ),
('Foo again' ) ) tc (columnss)
JOIN [dbo].[Delimitedsplit8k](@str, ' ') f
ON columnss LIKE '%' + f.Item + '%'
要创建拆分字符串功能,请检查此链接http://www.sqlservercentral.com/articles/Tally+Table/72993/
答案 1 :(得分:1)
任何分组/解析功能都可以提供帮助
Declare @YourTable table (YourFieldName varchar(100))
Insert Into @YourTable values
('Hi Bar'),
('Test Ba'),
('Hi Foo'),
('Nothing here'),
('Foo again')
Declare @SearchString varchar(max) = 'Foo Bar Test'
Select A.*
From @YourTable A
Join [dbo].[udf-Str-Parse](@SearchString,' ') B
on Charindex(RetVal,YourFieldName)>0
返回
YourFieldName
Hi Foo
Foo again
Hi Bar
Test Ba
UDF(如果需要)
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>'+ Replace(@String,@Delimiter,'</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')