SQL 将区号分配给 phone 号码的函数

SQL Function to assign areacode to phone number

是否可以创建一个函数,根据国家字段的值将 phone 区号 (+44) 分配给手机号码。

您可以从此处窃取国家 codes/country 缩写列表 https://countrycode.org/

使用此数据创建一个新的 table。看起来像:

Country | Country_Code

然后,我怀疑你有一个 table 类似的东西:

 Something | Phone_Number | Country

您现在只需要一些 SQL 将其组合在一起(不是函数):

SELECT existing_table.something, new_table.Country_code + " " + existing_table.phone_number
FROM existing_table
    JOIN new_table ON existing_table.country = new_table.country

鲍勃是你的叔叔。

您可以从这里 https://countrycode.org/ 窃取国家 codes/country 缩写列表,并使用身份列为它们分配国家 ID。

使用此数据创建一个新的 table。看起来像:

Country | Country_Code | Country_ID

然后,我怀疑你有一个 table 类似的东西:

 Something | Phone_Number | Country | Country_ID

更改现有 table 以添加 Country_ID,以提高性能:

ALTER TABLE existing_table 
ADD country_id int;

UPDATE e 
SET    e. country id = n.country_id 
FROM   existing_table e 
JOIN   new_table n 
ON     e.country = n.country

您现在只需要 SQL 将其组合在一起(不是函数):

SELECT e.something, 
       FORMAT(CONCAT(n.country_code,' ', e.phone_number), '###-##-####') AS CompletePhone
FROM   existing_table e
       JOIN new_table n
         ON e.country_id = n.country_id