简体   繁体   English

在Oracle中使用SQL查找带有意外字符的电话号码?

[英]Find phone numbers with unexpected characters using SQL in Oracle?

I need to find rows where the phone number field contains unexpected characters. 我需要找到电话号码字段包含意外字符的行。

Most of the values in this field look like: 该字段中的大多数值如下所示:

123456-7890

This is expected. 这是预期的。 However, we are also seeing character values in this field such as * and #. 但是,我们还在此字段中看到了字符值,例如*和#。

I want to find all rows where these unexpected character values exist. 我想找到所有存在这些意外字符值的行。

Expected: 预期:

  • Numbers are expected 预期数字
  • Hyphen with numbers is expected (hyphen alone is not) 带有数字的连字符是预期的(单独的连字符不是)
  • NULL is expected 预期为NULL
  • Empty is expected 预计为空

Tried this: 试过这个:

WHERE phone_num is not like ' %[0-9,-,' ' ]%

Still getting rows where phone has numbers. 仍然会在电话中显示行号。

from https://regexr.com/3c53v address you can edit regex to match your needs. 您可以从https://regexr.com/3c53v地址编辑正则表达式来满足您的需求。

I am going to use example regex for this purpose 我将为此使用示例正则表达式

select * from Table1
Where NOT REGEXP_LIKE(PhoneNumberColumn, '^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$')

You can use regexp_like() . 您可以使用regexp_like()

...
WHERE regexp_like(phone_num, '[^ 0123456789-]|^-|-$')

[^ 0123456789-] matches any character that is not a space nor a digit nor a hyphen. [^ 0123456789-]匹配任何非空格,数字或连字符的字符。 ^- matches a hyphen at the beginning and -$ on the end of the string. ^-匹配字符串的开头和结尾的-$ The pipes are "ors" ie a|b matches if pattern a matches of if pattern b matches. 管道是“或”,即如果模式a匹配,如果模式b匹配,则a|b b匹配。

You can use translate() 您可以使用translate()

...
WHERE translate(Phone_Number,'a1234567890-', 'a') is NOT NULL

This will strip out all valid characters leaving behind the invalid ones. 这将删除所有有效字符,并保留无效字符。 If all the characters are valid, the result would be NULL. 如果所有字符均有效,则结果将为NULL。 This does not validate the format, for that you'd need to use REGEXP_LIKE or something similar. 这不会验证格式,因为您需要使用REGEXP_LIKE或类似的格式。

Oracle has REGEXP_LIKE for regex compares: Oracle具有REGEXP_LIKE用于正则表达式比较:

WHERE REGEXP_LIKE(phone_num,'[^0-9''\-]')

If you're unfamiliar with regular expressions, there are plenty of good sites to help you build them. 如果您不熟悉正则表达式,那么有很多不错的网站可以帮助您构建它们。 I like this one 我喜欢这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM