简体   繁体   English

如何从SQL记录的电话号码中删除领先的国家代码?

[英]How to remove a leading country code from phone numbers in SQL records?

I am currently trying to figure out how to remove the leading country code from some phone number records within my SQL query. 我目前正在尝试找出如何从SQL查询中的某些电话号码记录中删除领先的国家/地区代码。 Essentially, some of the records that I am selecting have either a 1 (US) or 44 (UK) country code in front of the number. 本质上,我选择的某些记录的号码前面有1(美国)或44(英国)国家代码。 I would like to be able to detect which records have this country code and then separate it into a separate column. 我希望能够检测到哪些记录具有此国家/地区代码,然后将其分成单独的列。 Here is the current query that pulls the phone number data: 这是当前查询中提取电话号码数据的信息:

SELECT REPLACE(REPLACE(REPLACE(REPLACE(tbl_vsed_unvalidated.telephonenumber,'#',null),'-',null),' ',null),'+',null)
   AS "PHONE 1"

This query is pulling the data from a telephonenumber table column in the database and then stripping any #,-, and spaces. 该查询是从数据库中的电话号码表列中提取数据,然后剥离所有#,-和空格。 In order to try removing the country codes, I was considering making use of something like this: 为了尝试删除国家/地区代码,我正在考虑使用以下代码:

If(len>10){trim(Leading "1" from "PHONE 1")}

Would something like this work? 这样的事情行吗? Also how would I be able to store this 1 into a new entity called "PHONE 1 COUNTRY CODE"? 我又如何将这个1存储到名为“ PHONE 1 COUNTRY CODE”的新实体中?

Eventually I would need to be able to remove a trailing extension from records as well, but I assume that would be a similar process. 最终,我还需要能够从记录中删除尾随扩展名,但是我认为那将是一个类似的过程。 (Ex. if the number is 180012345671234, I would need to be able to strip the leading "1" and the trailing "1234" and store them each in new separate columns. (例如,如果数字为180012345671234,则我需要能够去除开头的“ 1”和结尾的“ 1234”,并将它们分别存储在新的单独的列中。

Thanks for any help! 谢谢你的帮助!

Pete 皮特

What i would suggest is 我建议的是

a for loop that goes through each number and detects the length if its +44 (UK) its going to be x length 一个for循环,遍历每个数字并检测其长度,如果它的+44(UK)为x长度

if its US it will be y length 如果是美国,则长度为

see if you can write something to match that up 看你是否可以写点东西来配合

then once you have a rough match, see if you can trim the numbers off while still in that loop. 然后在进行粗略匹配后,看看是否可以在仍处于该循环中的情况下修剪数字。

This is a basic example of using CASE to work through the phone numbers by lengtht. 这是一个使用CASE按长度浏览电话号码的基本示例。 You will have to change the lengths to suit your reality. 您将不得不更改长度以适合您的实际情况。 If your phone numbers have + or parens, etc, you should normalize them first so what remains is a string of numbers. 如果您的电话号码带有+或括号,等等,则应首先对其进行标准化,以便剩下的是一串数字。 As for the phones with extensions, they will show under the 'ELSE' clause and you can work out how to deal with them later, as you said. 至于带有扩展名的电话,它们将显示在“ ELSE”子句下,您可以按照您所说的,制定出以后如何处理的方法。 Hopefully this will at least get you started. 希望这至少可以帮助您入门。

SELECT 
   CASE phone
      WHEN Length(phone) = 10 THEN phone
      WHEN Length(phone)  = 11 THEN substr(phone,2)
      WHEN Length(phone)  = 14 THEN substr(phone,4)
      ELSE phone
   END AS Phone from customers 

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

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