none
Guys how can u split the complete contact number +CountryCode +AreaCode +Number into 3 textbox from a column of a table in database? RRS feed

  • Question

  • i have this format in my Contact Number column of table in database my numbers arranges like this for example +639228449586 which 63 is the country code and 922 is the first initial number of the mobile network provider and lastly the last 7 digit 8449586 is the ending number of the cellphone number. now my question is how can i populate them in 3 textboxes? i have this event in listview that when the indexes changes it will have to display the contact number column splitted in 3 textbox which i want my 63 in the txtCountryCode.Text and 922 in my txtAreaCode.Text and lastly the last numbers in my txtNumber.Text as well. how can i make it happen when its from database.

    thanks for your considerations 

    -maf
    Thursday, November 26, 2009 11:23 AM

Answers

  • Hi,
    
    You can use substring method of string in c# and substr function in t-sql
    
    Here is the code.
    
                string phonenumber = "+639228449586";
                string countrycode = phonenumber.Substring(1, 2);
                string initialnumber = phonenumber.Substring(3, 3);
                string phone = phonenumber.Substring(6);
                Console.WriteLine(phonenumber);
                Console.WriteLine(countrycode);
                Console.WriteLine(initialnumber);
                Console.WriteLine(phone);
                Console.Read();
                
    
                DECLARE @phonenumber nvarchar(20)
    SET @phonenumber='+639228449586'
    SELECT @phoneNumber AS PhoneNumber,
    SUBSTRING(@phoneNumber,2,2) AS CountryCode,
    SUBSTRING(@phoneNumber,4,3) AS InitialNumber,
    SUBSTRING(@phoneNumber,7,7) AS Phone
    • Proposed as answer by Geert van Horrik Monday, November 30, 2009 4:24 PM
    • Marked as answer by Harry Zhu Wednesday, December 2, 2009 2:53 AM
    Thursday, November 26, 2009 11:36 AM