简体   繁体   English

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

[英]Find phone numbers with unexpected characters using SQL

I posted the same question below for SQL in Oracle here and was provided the SQL info within that works. 我张贴同样的问题在下面在Oracle SQL 这里和范围内所提供之SQL信息。

However, I now need to perform the same in a DB2 database and if I attempt to run the same SQL it errors out. 但是,现在我需要在DB2数据库中执行相同的操作,如果尝试运行相同的SQL,则会出错。

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: 预期:

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

This SQL works in Oracle: 该SQL在Oracle中有效:

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

When using the same SQL above in DB2, the statement errors out. 在DB2中使用与上面相同的SQL时,该语句出错。

I found it easiest to answer your question by phrasing a regex which matches the positive cases. 我发现最容易通过与正例匹配的正则表达式来回答您的问题。 Then, we can just use NOT to find the negative cases. 然后,我们可以使用NOT查找否定情况。 DB2 supports a REGEXP_LIKE function: DB2支持REGEXP_LIKE函数:

SELECT *
FROM yourTable
WHERE
    NOT REGEXP_LIKE(phone_num, '^[0-9]+(-?[0-9]+)*$') AND
    COALESCE(phone_num, '') <> '';

Here is a demo of the regex: 这是正则表达式的演示:

Demo 演示版

For newer version of db2, regexp is the way to go. 对于db2的较新版本,regexp是必经之路。 If you are on an older version (perhaps why you get an error), you can replace all accepted chars with '' and check if the result is an empty string. 如果您使用的是旧版本(也许为什么会出现错误),则可以将所有接受的字符替换为'',然后检查结果是否为空字符串。 Can't check right now, but from memory, it would be 现在无法检查,但是从内存中可以

WHERE TRANSLATE(phone_num, '', '0123456789-')<>''

EDIT: 编辑:

For what it's worth your regexp works for V11 so you probably have an older version of Db2. 对于正则表达式,它可以在V11上使用,因此您可能拥有较旧的Db2版本。 Example of translate and regexp side by side: 并排翻译和正则表达式的示例:

]$ db2 "with t(s) as ( values '123456-7890', '12345*-7890' ) 
select s, 'regexp' as method from t 
where regexp_like(s, '[^ 0123456789-]|^-|-$') 
union all 
select s, 'translate' as method 
from t where TRANSLATE(s, '', '0123456789-')<>''"

S           METHOD   
----------- ---------
12345*-7890 translate
12345*-7890 regexp   

2 record(s) selected.

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

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