我有一张带电话号码的大桌子.电话号码都是字符串,应该是'+9628789878'或类似的.("+"符号后跟9到13位数字.)
用户错误发现了一行,字符串为"+ 987 + 9873678298".显然它不应该存在,我想知道有多少其他情况存在这种或其他此类错误.
我尝试了这个查询,但它没有完成这项工作.我的想法是任何不像这个字符串的东西.(哦,表格没有被phone_number编入索引.)
SELECT user_key,
first_name,
last_name,
phone_number
FROM users u
WHERE regexp_like(phone_number, '[^\+[0-9]*]')
AND phone_number IS NOT NULL
Run Code Online (Sandbox Code Playgroud)
Ale*_*sej 20
如果你需要找到所有phone_number
不完全由a '+'
后跟9-13位数的行,这应该做的工作:
select *
from users
where not regexp_like(phone_number, '^\+[0-9]{9,13}$')
Run Code Online (Sandbox Code Playgroud)
它能做什么:
^
字符串的开头,避免像这样的事情 'XX +123456789'
\+
'+'[0-9]{9,13}
一系列9-13位数字$
字符串的结尾,避免字符串之类的 '+123456789 XX'
另一种没有正则表达式的方法可能如下:
where not (
/* strings of 10-14 chars */
length(phone_number) between 10 and 14
/* ... whose first is a + */
and substr(phone_number, 1, 1 ) = '+'
/* ...and that become a '+' after removing all the digits */
and nvl(translate(phone_number, 'X0123456789', 'X'), '+') = '+'
)
Run Code Online (Sandbox Code Playgroud)
这可能比正则表达式更快,即使它基于更多条件,但我相信只有一个测试会告诉你哪一个是最佳表现.