CSV 字符串中每个变量的 SQL 搜索列 [英] SQL Search column for each variable in CSV string

查看:30
本文介绍了CSV 字符串中每个变量的 SQL 搜索列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量传递给存储过程例如:

I have a variable passed to stored procedure Ex:

@keywords = 'val1, val3, val5'

我正在尝试查看名为 Title 的列是否包含其中的任何一个

And i'm trying to see if column named Title contain any of them in it

Ex: Title1 - 'Hello val1'
    Title2 - 'Hello val3'   
    Title3 - 'Hello val1, val3'  
    Title4 - 'Hello' 

所以我的结果应该返回值

SO my results should return values

Title
------
Hello val1
Hello val3
Hello val1, val3

这可以使用 LIKE 或任何其他函数/方法吗?

Is this possible to use LIKE or any other function/method?

推荐答案

您需要将 CSV 拆分为行(请参阅 SQL Server 2005 及更高版本中的数组和列表 以了解各种技术如何).我假设您基于此创建 dbo.ufnSplitRows

You need to split the CSV into rows (see Arrays and Lists in SQL Server 2005 and Beyond for variuos techniques how). I'll assume that you create dbo.ufnSplitRows based on this

然后使用 LIKE 加入

Then JOIN using LIKE

SELECT *
FROM
    MYtable M
    JOIN
    dbo.ufnSplitRows (@CSV) C ON M.Title LIKE '%' + C.SplitValue + '%'

顺便说一句,它至少会因为前面的'%'而运行不佳

By the way, it will run poorly because of the leading '%' at least

这篇关于CSV 字符串中每个变量的 SQL 搜索列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆