SQL 如何检测字符串是否包含特殊字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2558755/
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
How to detect if a string contains special characters?
提问by Manish
How to detect if a string contains special characters like #,$,^,&,*,@,! etc (non-alphanumeric characters) in SQL server 2005?
如何检测字符串是否包含特殊字符,如#、$、^、&、*、@、!SQL Server 2005 中的等(非字母数字字符)?
回答by AdaTheDev
Assuming SQL Server:
假设 SQL Server:
e.g. if you class special characters as anything NOT alphanumeric:
例如,如果您将特殊字符归类为非字母数字:
DECLARE @MyString VARCHAR(100)
SET @MyString = 'adgkjb$'
IF (@MyString LIKE '%[^a-zA-Z0-9]%')
PRINT 'Contains "special" characters'
ELSE
PRINT 'Does not contain "special" characters'
Just add to other characters you don't class as special, inside the square brackets
只需在方括号内添加您不属于特殊类别的其他字符
回答by Brendan Long
SELECT * FROM tableName WHERE columnName LIKE "%#%" OR columnName LIKE "%$%" OR (etc.)
回答by skyman
In postgresql you can use regular expressions in WHERE clause. Check http://www.postgresql.org/docs/8.4/static/functions-matching.html
在 postgresql 中,您可以在 WHERE 子句中使用正则表达式。检查http://www.postgresql.org/docs/8.4/static/functions-matching.html
MySQL has something simmilar: http://dev.mysql.com/doc/refman/5.5/en/regexp.html
MySQL 有类似的东西:http://dev.mysql.com/doc/refman/5.5/en/regexp.html