如何匹配包含多个值的列中的搜索字符串的任何值,这些值由sql中的表空格分隔?

时间:2016-10-06 13:25:49

标签: csv sql-server-2012 sql-like

我在column中有一个table,其中有多个值由space分隔。 我想返回那些具有搜索字符串中任何匹配值的rows

例如:

搜索字符串= 'mumbai pune'

这需要返回与单词'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

2 个答案:

答案 0 :(得分:1)

您可以使用拆分器功能将搜索字符串拆分为包含所需搜索键的表格。然后,您可以使用LIKE语句将包含搜索关键字的表加入主表。

为了完整性,我已经包含了一个字符串拆分器功能的例子,但是在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

答案 1 :(得分:1)

另一种方法,使用Replace函数

其语法如下:

  

REPLACE(string_expression,string_pattern,string_replacement)

所以我们可以通过替换用下一个模式分隔值的每个空间来达到目标​​

 '%'' 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

结果:

enter image description here