Sql Server 在另一个字符串中查找部分字符串

标签 sql sql-server

我有一张 table

studentId        Classes
1               Auto CAD,Electronics
2               Instrumentation,Chemical,IT,English
3               Computer,Math,Hindi
4               Physics,Accountancy
5               Bengali,Geography

现在我想使用单个字符串“Instrumentation,Geography,Physics”进行搜索

它将显示以下内容

studentId        Classes
2               Instrumentation,Chemical,IT,English
4               Physics,Accountancy
5               Bengali,Geography

因为studentid 2包含Instrumentation,studentid 4包含Physics,studentid 5包含Geography

如何使用 SELECT 语句来完成此操作?不像“%Instrumentation%”这样的类 或像“%Geography%”或像“%Physics%”这样的类,因为我想使用单个变量来字符串“Instrumentation,Geography,Physics”或用逗号分隔的字符串的任何组合。

最佳答案

试试这个:

SELECT * 
FROM    your_Table
WHERE   ','+Classes+',' like '%,Instrumentation,%'
OR      ','+Classes+',' like '%,Geography,%'
OR      ','+Classes+',' like '%,Physics,%'

我在列和搜索条件中添加了一个逗号,以便它只返回以逗号结尾的字符串

关于Sql Server 在另一个字符串中查找部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384334/

相关文章:

SQL - 将来自多个表的汇总数据连接在一起

c# - 将列数据存储为行 MS SQL 的最佳方法

sql - 如何在不禁用 CDC 的情况下在基础表的新添加列上激活变更数据捕获 (CDC)

sql-server - 带有前导通配符的参数化查询的 SQL Server 性能

sql - 如何将行旋转到具有已知最大列数的列

sql - 使用 Microsoft sql server 2012 遇到基本 SQL 问题

c# - 使用参数正确保护 SQL 语句

c# - TransactionScope 错误保存大量信息

sql - 在 reddit 评论数据集中 - 哪些帖子的评论数上升到 1000 条最快?

sql - 简化的 SQL Min 语句