SQL Server, combining LIKE and IN?

时间秒杀一切 提交于 2019-11-27 07:04:56

问题


Is there an easy way to combine LIKE and IN in one statement in SQL Server, without using a lot of AND and OR?

e.g. I know in MySQL you can do it this way:

SELECT * FROM table1 WHERE column1 REGEXP 'value1|value2|value3'

回答1:


Not really.

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

You can also use Regular Expressions in SQL Server but not natively. You need to enable CLR and install an assembly for this.




回答2:


Another option would be to put the search values in a table and build a dynamic SQL to do the work. It is not recommended but sometimes helps...



来源:https://stackoverflow.com/questions/8408900/sql-server-combining-like-and-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!