简体   繁体   English

如何使用SQL替换电话号码中的某些字符?

[英]How to replace some characters in phone number using SQL?

I have a table Contact containing all user contacts with phone numbers and need to do some transformations for phone numbers. 我有一个表Contact包含所有用户联系人的电话号码,需要对电话号码进行一些转换。

I need to go through the all phone numbers and: 我需要通过所有电话号码和:

  1. remove following characters sequence (0) when present in the phone number; 当电话号码存在时,删除后面的字符序列(0) ;

  2. add the prefix +<country_code> when is missing and when a telephone number has been inserted; 缺少时添加前缀+<country_code>以及插入电话号码时;

Example: 例:

The phone number: 电话号码:
+1 (0) 121 121 121
needs to be transformed to: 需要转变为:
+1 121 121 121

The phone number: 电话号码:
(0) 121 121 121
needs to be transformed to: 需要转变为:
+1 121 121 121

The phone number: 电话号码:
121 121 121
needs to be transformed to: 需要转变为:
+1 121 121 121

According to the point 1: 根据第1点:

We can select all phone numbers with query: 我们可以通过查询选择所有电话号码:

select phone from contact where phone like '%(0)%';

but how to remove that sequence (0) only from the number? 但是如何仅从数字中删除该序列(0) How to create an update query for that? 如何为此创建更新查询?

According to the point 2: 根据第2点:

How to recognize if the phone number do not contain country direct number (prefix +<country_code> ) and add a correct one if is missing? 如何识别电话号码是否不包含国家直拨号码(前缀+<country_code> ),如果丢失则添加正确的号码? Maybe this query should be fine to select those numbers: 也许这个查询应该可以选择这些数字:

select phone from contact where phone not like '%+%';

We can assume that. 我们可以假设。 Let's assume also that we have a column country in Contact table and for the Country Code List we can create temporary mapping table based on this -> http://countrycode.org/ . 我们假设我们在Contact表中有一个列国家,对于国家代码列表,我们可以基于此创建临时映射表 - > http://countrycode.org/ We can simply create a temporary table containing country code <-> country prefix mapping as I showed below. 我们可以简单地创建一个包含国家代码< - >国家/地区前缀映射的临时表,如下所示。

I guess that it should be possible to call an update query which in 1st step will select the all phone numbers in wrong format and in 2nd step will update with the new correct value, right? 我想应该可以调用更新查询,在第一步中将选择错误格式的所有电话号码,第二步将使用新的正确值更新,对吗? I do not know SQL very well, so please help me to create such a SQL query? 我不太了解SQL,所以请帮我创建这样的SQL查询?

Table CONTACT: 表联系方式:

+-----+-----------+----------+---------+------------------------+
| id  | firstname | lastname | country | phone                  |
+-----+-----------+----------+---------+------------------------+
| 100 | Frank     | Grob     |   PL    | +48 22 121 121 121     | <- OK
| 101 | Bob       | Bloby    |   PL    | (0)22 121 121 121      | <- Wrong
| 102 | Alice     | Wonder   |   US    | +1 (0) 121 121 121     | <- Wrong
| 103 | Chris     | Black    |   US    | +1 (0) 121 121 121     | <- Wrong
| 104 | Rocky     | Rocky    |   US    |  +1 (0) 121 121 121    | <- Wrong
+-----+-----------+----------+---------+------------------------+

Table COUNTRY_MAPPING: 表COUNTRY_MAPPING:

+-----+--------------+--------+
| id  | country_code | prefix |
+-----+--------------+--------+
| 100 | PL           | 48     |
| 101 | US           | 1      |
+-----+--------------+--------+

This should not be difficult. 这应该不难。 If you have a country code column in your CONTACT table and a separate table COUNTRY_MAPPING , then such a query might look like this (FYI, Poland's prefix is 48 as you correctly have in your CONTACT table, not 22 as you have in your COUNTRY_MAPPING table): 如果你的CONTACT表中有一个国家代码列和一个单独的表COUNTRY_MAPPING ,那么这样的查询可能如下所示(FYI,波兰的前缀是你在CONTACT表中正确的48 ,而不是你在COUNTRY_MAPPING表中的22 ):

SELECT c.id, c.firstname, c.lastname, c.country, c.phone
     , REGEXP_REPLACE(REGEXP_REPLACE(c.phone, '\(0\)\s*'), '^([^+])', '+' || cm.prefix || ' \1') AS new_phone
  FROM contact c, country_mapping cm
 WHERE REGEXP_LIKE(c.phone, '(^[^+]|\(0\))')
   AND c.country = cm.country_code

Please see SQL Fiddle demo here. 请在此处查看SQL Fiddle演示。

For an UPDATE I would recommend the following: 对于UPDATE我建议如下:

1. Create a temporary table based on the query above: 1.根据上面的查询创建一个临时表:

CREATE TABLE contact_newphone AS
SELECT c.id, c.firstname, c.lastname, c.country, c.phone
     , REGEXP_REPLACE(REGEXP_REPLACE(c.phone, '\(0\)\s*'), '^([^+])', '+' || cm.prefix || ' \1') AS new_phone
  FROM contact c, country_mapping cm
 WHERE REGEXP_LIKE(c.phone, '(^[^+]|\(0\))')
   AND c.country = cm.country_code

2. Update from that temporary table: 2.从该临时表更新:

UPDATE contact c
   SET c.phone = ( SELECT cn.newphone FROM contact_newphone cn
                    WHERE cn.id = c.id )
 WHERE REGEXP_LIKE(c.phone, '(^[^+]|\(0\))') -- don't want to update anyone's phone# that might have been fixed!
   AND EXISTS ( SELECT 1 FROM contact_newphone cn
                 WHERE cn.id = c.id )

3. Drop the "temporary" table - or keep it as a backup of the old, bad phone numbers. 3.删除“临时”表 - 或将其作为旧电话号码的备份。

UPDATE: If you have leading spaces in the phone column, you might do the following in step 1 更新:如果phone列中有前导空格,则可以在步骤1中执行以下操作

CREATE TABLE contact_newphone AS
SELECT c.id, c.firstname, c.lastname, c.country, c.phone
     , REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(c.phone, '^\s+'), '\(0\)\s*'), '^([^+])', '+' || cm.prefix || ' \1') AS new_phone
  FROM contact c, country_mapping cm
 WHERE REGEXP_LIKE(c.phone, '(^[^+]|\(0\))')
   AND c.country = cm.country_code

Please see SQL Fiddle demo here. 请在此处查看SQL Fiddle演示。 This will also remove leading spaces from phone numbers that are otherwise good. 这也将从电话号码中移除前导空格,否则这些空间是好的。

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

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