SQL Not Like 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3648644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Not Like Statement
提问by NETQuestion
How can I select table from sys.tables where table name does not containt special word (which pass in parameters).
如何从 sys.tables 中选择表,其中表名不包含特殊字(传入参数)。
I want to select all tables where its contains word 'customer' but not those who ends with 'old' in name.
我想选择所有包含单词“customer”的表,但不选择名称以“old”结尾的表。
TableName In DB
数据库中的表名
customer1
customer2
customer3
customerold1
customerold2
客户1
客户2
客户3
客户
旧1客户旧2
Output Wanted
想要的输出
customer1
customer2
customer3
客户1
客户2
客户3
回答by Michael Petrotta
SELECT * FROM sys.tables
WHERE TableName LIKE '%customer%'
AND TableName NOT LIKE '%old' -- note the lack of trailing '%'
回答by NETQuestion
Assuming that you have a parameter for your special word, you can do:
假设您有一个特殊词的参数,您可以执行以下操作:
WHERE TableName LIKE @specialWord + '%'
AND TableName NOT LIKE '%' + @specialWord + '%old%'