How to split mobile number into country code, area code and local number?

前端 未结 7 574
悲&欢浪女
悲&欢浪女 2020-12-03 10:33

How to split mobile number into country code, area code and local number? e.g +919567123456 after split

country code = 91

area code = 9567

local numb

7条回答
  •  既然无缘
    2020-12-03 11:16

    As mentioned by various people you can not do this with simple string matching. The lengths of neither country nor area codes are fixed.

    Having done this in the past we maintained a table similar in structure to the following :-

    +------------+---------+-------+--------------+
    |country_code|area_code|country|area          |
    +------------+---------+-------+--------------+
    |44          |1634     |UK     |Medway        |
    |44          |20       |UK     |London        |
    |964         |23       |Iraq   |Wasit (Al Kut)|
    |964         |2412     |Iraq   |Unreal        |
    +------------+---------+-------+--------------+
    

    We then calculated the maximum length of area_code and country_code and checked the string by sub-stringing starting at the maximum length and working our way down until we found a match.

    So given the number 441634666788

    We would have started at the substring[1,7] (7 being the length of the longest country/area code combination), not found a match, then moved on to [1,6] and found the match for UK/Medway.

    Not very efficient but it worked.

    EDIT

    You could also try something like this but you would need to test it with a full data set or maybe even break it down into separate country and area code selects as it may not be very performant with your chosen DB.

     DECLARE @area_codes TABLE
    (
        country_code VARCHAR(10),
        area_code VARCHAR(10),
        country VARCHAR(20),
        area VARCHAR(20),
        match_string VARCHAR(MAX),
        match_length INTEGER
    )
    
    INSERT INTO @area_codes VALUES ('44','1382','UK','Dundee', '441382%', 6)
    INSERT INTO @area_codes VALUES ('44','1386','UK','Evesham', '441386%', 6)
    INSERT INTO @area_codes VALUES ('44', '1', 'UK', 'Geographic numbers', '441%', 3)
    
    DECLARE @number VARCHAR(MAX)
    SET @number = '441386111111'
    
    SELECT TOP 1 * 
    FROM @area_codes 
    WHERE @number LIKE match_string
    ORDER BY match_length DESC
    

    You would maintain the match_string and match_length fields through a trigger, taking care to cope with null area codes and index the table on the match_string column.

提交回复
热议问题