如何从 sql 中的 table 中包含由 space 分隔的多个值的列中匹配搜索字符串的任何值?
How to match any value of search string from a column containing multiple values separated by space in table in sql?
我在 table
中有一个 column
,它有多个值,由 space
分隔。
我想要 return 那些 rows
具有来自搜索字符串的任何匹配值。
例如:
搜索字符串= 'mumbai pune'
这需要return行匹配词'mumbai'
或'pune'
或匹配both
Declare @str nvarchar(500)
SET @str='mumbai pune'
create table #tmp
(
ID int identity(1,1),
citycsv nvarchar(500)
)
insert into #tmp(citycsv)Values
('mumbai pune'),
('mumbai'),
('nagpur')
select *from #tmp t
select *from #tmp t
where t.citycsv like '%'+@str+'%'
drop table #tmp
需要输出:
ID CityCSV
1 mumbai pune
2 mumbai
您可以使用拆分器功能将搜索字符串拆分为 table 包含所需的搜索键。然后,您可以使用 LIKE
语句将主 table 与包含搜索键的 table 连接起来。
为了完整起见,我包含了一个字符串拆分器函数的示例,但是这里有很多关于 SO 的示例。
示例字符串拆分器函数:
CREATE FUNCTION [dbo].[SplitString]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (splitdata)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
以下查询演示了如何将字符串拆分器函数与正则表达式组合以获得所需的结果:
SELECT DISTINCT
C.ID
,C.citycsv
FROM #tmp C
INNER JOIN (
SELECT splitdata + '[ ]%' AS MatchFirstWord -- Search pattern to match the first word in the string with the target search word.
,'%[ ]' + splitdata AS MatchLastWord -- Search pattern to match the last word in the string with the target search word.
,'%[ ]' + splitdata + '[ ]%' AS MatchMiddle -- Search pattern to match any words in the middle of the string with the target search word.
,splitdata AS MatchExact -- Search pattern for exact match.
FROM dbo.SplitString(@str, ' ')
) M ON (
(C.citycsv LIKE M.MatchFirstWord) OR
(C.citycsv LIKE M.MatchLastWord) OR
(C.citycsv LIKE M.MatchMiddle) OR
(C.citycsv LIKE M.MatchExact)
)
ORDER BY C.ID
另一种方法,使用 Replace
函数
其语法如下:
REPLACE ( string_expression , string_pattern , string_replacement )
因此我们可以通过用下一个模式
替换分隔值的 每个 space 来达到目标
'%'' OR t.citycsv like ''%'
例子:
Declare @str nvarchar(500),
@Where nvarchar (1000),
@Query nvarchar (4000)
SET @str='mumbai pune'
create table #tmp
(
ID int identity(1,1),
citycsv nvarchar(500)
)
insert into #tmp(citycsv)Values
('mumbai pune'),
('mumbai'),
('nagpur')
select * from #tmp t
Set @Where = 'where t.citycsv like ' + '''%'+ replace (RTRIM(LTRIM(@str)), ' ', '%'' OR t.citycsv like ''%') +'%'''
Set @Query = 'select * from #tmp t ' + @Where
execute sp_executesql @Query
drop table #tmp
结果:
我在 table
中有一个 column
,它有多个值,由 space
分隔。
我想要 return 那些 rows
具有来自搜索字符串的任何匹配值。
例如:
搜索字符串= 'mumbai pune'
这需要return行匹配词'mumbai'
或'pune'
或匹配both
Declare @str nvarchar(500)
SET @str='mumbai pune'
create table #tmp
(
ID int identity(1,1),
citycsv nvarchar(500)
)
insert into #tmp(citycsv)Values
('mumbai pune'),
('mumbai'),
('nagpur')
select *from #tmp t
select *from #tmp t
where t.citycsv like '%'+@str+'%'
drop table #tmp
需要输出:
ID CityCSV
1 mumbai pune
2 mumbai
您可以使用拆分器功能将搜索字符串拆分为 table 包含所需的搜索键。然后,您可以使用 LIKE
语句将主 table 与包含搜索键的 table 连接起来。
为了完整起见,我包含了一个字符串拆分器函数的示例,但是这里有很多关于 SO 的示例。
示例字符串拆分器函数:
CREATE FUNCTION [dbo].[SplitString]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (splitdata)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
以下查询演示了如何将字符串拆分器函数与正则表达式组合以获得所需的结果:
SELECT DISTINCT
C.ID
,C.citycsv
FROM #tmp C
INNER JOIN (
SELECT splitdata + '[ ]%' AS MatchFirstWord -- Search pattern to match the first word in the string with the target search word.
,'%[ ]' + splitdata AS MatchLastWord -- Search pattern to match the last word in the string with the target search word.
,'%[ ]' + splitdata + '[ ]%' AS MatchMiddle -- Search pattern to match any words in the middle of the string with the target search word.
,splitdata AS MatchExact -- Search pattern for exact match.
FROM dbo.SplitString(@str, ' ')
) M ON (
(C.citycsv LIKE M.MatchFirstWord) OR
(C.citycsv LIKE M.MatchLastWord) OR
(C.citycsv LIKE M.MatchMiddle) OR
(C.citycsv LIKE M.MatchExact)
)
ORDER BY C.ID
另一种方法,使用 Replace
函数
其语法如下:
REPLACE ( string_expression , string_pattern , string_replacement )
因此我们可以通过用下一个模式
替换分隔值的 每个 space 来达到目标 '%'' OR t.citycsv like ''%'
例子:
Declare @str nvarchar(500),
@Where nvarchar (1000),
@Query nvarchar (4000)
SET @str='mumbai pune'
create table #tmp
(
ID int identity(1,1),
citycsv nvarchar(500)
)
insert into #tmp(citycsv)Values
('mumbai pune'),
('mumbai'),
('nagpur')
select * from #tmp t
Set @Where = 'where t.citycsv like ' + '''%'+ replace (RTRIM(LTRIM(@str)), ' ', '%'' OR t.citycsv like ''%') +'%'''
Set @Query = 'select * from #tmp t ' + @Where
execute sp_executesql @Query
drop table #tmp
结果: