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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:52:22  来源:igfitidea点击:

How to detect if a string contains special characters?

sqlsql-server

提问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