简体   繁体   English

Oracle SQL 查询查找电话号码的特殊字符

[英]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>

You can use [[:punct:]] posix along with REGEXP_REPLACE() such as您可以将[[:punct:]] posix 与REGEXP_REPLACE()一起使用,例如

SELECT REGEXP_REPLACE(col,'[[:punct:]]') AS col
  FROM t

assuming each comma-separated value represents a column value within a table假设每个逗号分隔值代表表中的一个列值

Demo演示

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 SIMPLIFIED_PHONE_NUMBER
2047653894 2047653894
2047653894 2047653894
2047653894 2047653894
2047653894 2047653894
2047653894 2047653894

fiddle小提琴


Update更新

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 )慢。

fiddle小提琴

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

相关问题 在Oracle中使用SQL查找带有意外字符的电话号码? - Find phone numbers with unexpected characters using SQL in Oracle? SQL 代码查找和抑制数字和特殊字符 - SQL code to find and suppress numbers and special characters 如何在 Oracle SQL 中找到 col 具有特殊字符或数字(连字符、撇号和空格除外)的行 - How to find a row where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL 如何在 Oracle SQL 中找到 col 有字母、数字或特殊字符(连字符、撇号和空格除外)的行 - How to find a row where col have alphabets,numbers or special characters (except hyphen,apostrophe and space) in Oracle SQL 使用SQL查找带有意外字符的电话号码 - Find phone numbers with unexpected characters using SQL oracle sql - 查询查找特殊字符 - oracle sql - query to find special chars 如何在一个oracle sql语句中删除空格、特殊字符和数字 - how to remove spaces, special characters and numbers in one oracle sql statement Oracle SQL查询以查找字符串中是否存在字符 - Oracle SQL Query to find the existence of characters in string Oracle SQL - 在字段中查找特殊的非数字字符 - Oracle SQL - Find special non numeric characters in field 用于排除名称末尾的数字、括号或特殊字符的 SQL 查询 - SQL query to exclude numbers, brackets or special characters at the end of a name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM