Problem Statement:
I need to replace part of the string from the address column in SQL query(script),preferably phone number, where i could see around 91 patterns.What's is the best way to achieve this.?
Earlier I had the similar issue but I was dealing with only 5 different patterns, so I was able to do this by using nested replace like: replace(replace(replace()))
But now I have around 91 different patterns to update, so I was looking for the better way to accomplish this. I'm also dealing here with data of around 10000 rows.
Basically I need to update phone numbers for Wells Fargo as: 800-848-9492
Actual Data for 2 rows in Address Column looks like:
- Wells Fargo: 800-841-7254 Huntington Mortgage: 800-262-4532
- Wells Fargo: (800) 456-7654 Huntington Mortgage: (800) 451-8763
91 patterns looks similar to below:(Common thing we have is Wells Fargo)
Wells Fargo: 800-841-7254
Wells Fargo: (800) 456-7654
Wells Fargo: 1-866-876-1234
Wells Fargo: (800) 654-7654
Wells Fargo: 866-213-3211
These should be replaced as:
Wells Fargo: 800-848-9492
Wells Fargo: 800-848-9492
Wells Fargo: 800-848-9492
Wells Fargo: 800-848-9492
Wells Fargo: 800-848-9492
Actual Data for 2 rows in Address Column should look like:
Wells Fargo: 800-848-9492 Huntington Mortgage: 800-262-4532
Wells Fargo: 800-848-9492 Huntington Mortgage: (800) 451-8763
I'm trying to achieve this by using the Table variable with Phone Numbers and replacing like below:
Replace in the below query is giving syntax error like multi-part identifier could not be found.
Do I need to make some kind of join with table variable..??
DECLARE @tblPhoneNumbers table (data varchar(100))
Insert @tblPhoneNumbers select '800-841-7254'
Insert @tblPhoneNumbers select '1-866-876-1234'
Insert @tblPhoneNumbers select '(800) 654-7654'
Update VDV SET
VDV.AttribValueTxt=REPLACE(VDV.AttribValueTxt,P1.data,'800-848-9492') from @tblPhoneNumbers P1,
VDV.UpdateDt=VDV.UpdateDt,
VDV.UpdateId=@UpdateId
FROM [dbo].[tblCliPolVerProdDeliveryAttribValue](NOLOCK) AS VDV
INNER JOIN [dbo].[tblCliVerProdDelttribute](NOLOCK) as VDA ON VDV.AttribId=VDA.AttribId
INNER JOIN [dbo].[tblPolMasDelAttribute](NOLOCK) as MDA ON VDA.AttribId=MDA.AttribId
Where MDA.AttribId IN(1246,1270) AND DeliveryAttribValueTxt IS NOT NULL