SQL Server: 2008 R2 Version
I need a t-sql query to remove country code from all the phone numbers that has prefix code like if the phone number is 8522345678901 for hong kong, I want to remove 852 and the result should be 2345678901. The below tables are below and connecting column is country code i.e HK, USA, UK, AUS so based on the country code, I need to trim the prefix DialingPhoneCode from PhoneNumbers table columns i.e mobile, fax, landline.
Sample Data for PhoneNumbers
Mobile , Fax, Landline, CountryCode
61298765432, 228765432 , 598765432, AUS
61298765432, 61228765432, 598765432, AUS
85228157711, 28157711 , 85228157711,HK
Sample Data for PhoneCodes
DialingPhoneCode, CountryCode
61 , AUS
851, HK
Expected Output for PhoneNumbers
Mobile,Fax, Landline, CountryCode
298765432, 228765432, 598765432, AUS
298765432, 228765432, 598765432, AUS
28157711 , 28157711 , 28157711 , HK
Microsoft T-SQL
CREATE TABLE [dbo].[PhoneNumbers](
[Mobile] [varchar](500) NULL,
[Fax] [varchar](500) NULL,
[Landline] [varchar](500) NULL,
[CountryCode] [varchar](500) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[PhoneCodes](
[DialingPhoneCode] [varchar](500) NULL,
[CountryCode] [varchar](500) NULL
) ON [PRIMARY]
GO
01234567890would be written as+441234567890, but if you were calling it, it would be00441234567890if you're dialing from Europe; however, it would be011441234567890if you're in the USA. If they don't always have a country code, how do you know what does, and doesn't?