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

Csv 如何匹配sql表中包含多个以空格分隔的值的列中搜索字符串的任何值?,csv,sql-server-2012,sql-like,Csv,Sql Server 2012,Sql Like,我在表中有一个列,它有多个由空格分隔的值。 我想返回那些具有搜索字符串中任何匹配值的行 例如: 搜索字符串=“孟买浦那” 这需要返回匹配单词'mumbai'或'pune'或匹配两者的行 Declare @str nvarchar(500) SET @str='mumbai pune' create table #tmp ( ID int identity(1,1), citycsv nvarchar(500) ) insert into #tmp(citycsv)Values ('mumb

我在
中有一个
,它有多个由
空格
分隔的值。 我想返回那些具有搜索字符串中任何匹配值的

例如:

搜索字符串=
“孟买浦那”

这需要返回匹配单词
'mumbai'
'pune'
或匹配
两者的行

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

可以使用拆分器函数将搜索字符串拆分为包含所需搜索键的表。然后可以使用
LIKE
语句将主表与包含搜索键的表连接起来

为了完整起见,我提供了一个字符串拆分器函数的示例,不过这里有很多这样的示例

字符串拆分器函数示例:

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

另一种方法是使用
Replace
函数

其语法如下:

替换(字符串表达式、字符串模式、字符串替换)

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

 '%'' OR t.citycsv like ''%'
一个例子:

ID CityCSV
1  mumbai pune
2  mumbai
    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
结果:

ID CityCSV
1  mumbai pune
2  mumbai
    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

好极了!谢谢@Edmond Quinton+1为您服务。下面是一个sql提琴: