返回字段仅包含非字母数字字符的 sql 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7157389/
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 11:48:45  来源:igfitidea点击:

Return sql rows where field contains ONLY non-alphanumeric characters

sqlsql-serverregextsql

提问by marcusstarnes

I need to find out how many rows in a particular field in my sql server table, contain ONLY non-alphanumeric characters.

我需要找出我的 sql server 表中特定字段中有多少行,只包含非字母数字字符。

I'm thinking it's a regular expression that I need along the lines of [^a-zA-Z0-9] but Im not sure of the exact syntax I need to return the rows if there are no valid alphanumeric chars in there.

我认为这是一个正则表达式,我需要 [^a-zA-Z0-9] 行,但我不确定如果那里没有有效的字母数字字符,我需要返回行的确切语法。

回答by gbn

SQL Server doesn't have regular expressions. It uses the LIKE pattern matching syntax which isn't the same.

SQL Server 没有正则表达式。它使用不同的 LIKE 模式匹配语法。

As it happens, you are close. Just need leading+trailing wildcards and move the NOT

碰巧,你很接近。只需要前导+尾随通配符并移动NOT

 WHERE whatever NOT LIKE '%[a-z0-9]%'

回答by Crack

If you have short strings you should be able to create a few LIKE patterns ('[^a-zA-Z0-9]', '[^a-zA-Z0-9][^a-zA-Z0-9]', ...) to match strings of different length. Otherwise you should use CLR user defined function and a proper regular expression - Regular Expressions Make Pattern Matching And Data Extraction Easier.

如果您有短字符串,您应该能够创建一些 LIKE 模式('[^a-zA-Z0-9]', '[^a-zA-Z0-9][^a-zA-Z0-9]', ...)来匹配不同长度的字符串。否则,您应该使用 CLR 用户定义函数和适当的正则表达式 -正则表达式使模式匹配和数据提取更容易