SQL Server,结合 LIKE 和 IN?
问题描述:
是否有一种简单的方法将 LIKE
和 IN
组合在 SQL Server 中的一个语句中,而无需使用大量的 AND
和 >OR
?
Is there an easy way to combine LIKE
and IN
in one statement in SQL Server, without using a lot of AND
and OR
?
例如我知道在 MySQL 中你可以这样做:
e.g. I know in MySQL you can do it this way:
SELECT * FROM table1 WHERE column1 REGEXP 'value1|value2|value3'
答
并非如此.
LIKE
模式语法中没有交替运算符.如果在 2008 年你可以使用
There is no alternation operator in the LIKE
pattern syntax. If on 2008 you can use
SELECT *
FROM table1
WHERE EXISTS(SELECT *
FROM (VALUES ('value1'),
('value2'),
('value3')) Vals(val)
WHERE column1 LIKE '%' + val + '%')
您也可以在 SQL Server 中使用正则表达式,但不能在本机使用.您需要启用 CLR 并为此安装程序集.
You can also use Regular Expressions in SQL Server but not natively. You need to enable CLR and install an assembly for this.