Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better SQL Replace

I wanted to know if there is a better way of writing this Update.

update Alumni_Export_New
set phone = replace(replace(replace(replace(phone,'-',''),' ',''),')',''),'(','')
from tbl

I have a stored procedure in MSSQL 2005 to clean the phone field in a table and thought there should be a better way of doing this then what I have.

like image 888
Sandy DeLeon Avatar asked Jun 16 '26 10:06

Sandy DeLeon


2 Answers

Use CLR integration and Regular Expressions.

like image 55
Martin Smith Avatar answered Jun 19 '26 02:06

Martin Smith


You can consider putting the replace logic in a user defined function if you really don't like the look the REPLACE and if you don't mind a performance hit. Otherwise, what you have is probably your best solution.

Sample:

UPDATE Alumni_Export_New
SET phone = dbo.StripPhoneNumber(phone)
FROM tbl
like image 29
Phil Hunt Avatar answered Jun 19 '26 03:06

Phil Hunt