[英]Oracle SQL query to find special characters for phone numbers
I am trying to write a query to find special character for phone numbers.我正在尝试编写一个查询来查找电话号码的特殊字符。
Expected phone number is: 2047653894
预计电话号码是:
2047653894
Actual: 204765389(4
, 204-7653894
, -2047653894
, (204)7653894
, 20476+53894
, ....实际:
204765389(4
, 204-7653894
, -2047653894
, (204)7653894
, 20476+53894
, ....
NOTE: I only want to find the phone numbers with special characters.注意:我只想查找带有特殊字符的电话号码。 I don't want to replace special characters.
我不想替换特殊字符。
Another option is to remove all non-digits (here, where I live, phone numbers are digits only ; I'm not talking about various formats phone numbers might have):另一种选择是删除所有非数字(在这里,我住的地方,电话号码只是数字;我不是在谈论电话号码可能具有的各种格式):
SQL> with test (col) as
2 (select '204765389(4' from dual union all
3 select '204-7653894' from dual union all
4 select '-2047653894' from dual union all
5 select '(204)7653894' from dual union all
6 select '20476+53894' from dual
7 )
8 select
9 col,
10 regexp_replace(col, '\D') result
11 from test;
COL RESULT
------------ ------------------------------------------------
204765389(4 2047653894
204-7653894 2047653894
-2047653894 2047653894
(204)7653894 2047653894
20476+53894 2047653894
SQL>
[EDIT] [编辑]
If you just want to find phone numbers that contain anything but digits, use regexp_like
:如果您只想查找不包含数字的电话号码,请使用
regexp_like
:
SQL> with test (col) as
2 (select '204765389(4' from dual union all
3 select '204-7653894' from dual union all
4 select '-2047653894' from dual union all
5 select '(204)7653894' from dual union all
6 select '20476+53894' from dual union all
7 select '2047653897' from dual
8 )
9 select col
10 from test
11 where regexp_like(col, '\D');
COL
------------
204765389(4
204-7653894
-2047653894
(204)7653894
20476+53894
SQL>
While you can use regular expressions, they are slow and it may be faster to use simple string functions and use TRANSLATE
to find all the non-numeric characters and then replace them:虽然您可以使用正则表达式,但它们很慢,使用简单的字符串函数并使用
TRANSLATE
查找所有非数字字符然后替换它们可能会更快:
SELECT TRANSLATE(
phone_number,
'0' || TRANSLATE(phone_number, 'x0123456789', 'x')
'0'
) AS simplified_phone_number
FROM table_name;
Which, for your sample data:其中,对于您的示例数据:
CREATE TABLE table_name (phone_number) AS
SELECT '204765389(4' FROM DUAL UNION ALL
SELECT '204-7653894' FROM DUAL UNION ALL
SELECT '-2047653894' FROM DUAL UNION ALL
SELECT '(204)7653894' FROM DUAL UNION ALL
SELECT '20476+53894' FROM DUAL;
Outputs:输出:
SIMPLIFIED_PHONE_NUMBER ![]() |
---|
2047653894 ![]() |
2047653894 ![]() |
2047653894 ![]() |
2047653894 ![]() |
2047653894 ![]() |
If you want to list phone numbers with non-digit characters then you can also use TRANSLATE
to remove the digits and check if there are any other characters:如果你想列出非数字字符的电话号码,那么你也可以使用
TRANSLATE
删除数字并检查是否有任何其他字符:
SELECT *
FROM table_name
WHERE TRANSLATE(phone_number, 'x0123456789', 'x') IS NOT NULL
you could also use REGEXP_LIKE
to check that the string is not entirely digits:您还可以使用
REGEXP_LIKE
检查字符串是否不完全是数字:
SELECT *
FROM table_name
WHERE NOT REGEXP_LIKE(phone_number, '^\d+$')
or that there are non-digits:或者有非数字:
SELECT *
FROM table_name
WHERE REGEXP_LIKE(phone_number, '\D')
However, regular expressions are probably going to be slower than simple string functions like TRANSLATE
.但是,正则表达式可能会比简单的字符串函数(如
TRANSLATE
)慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.