Link to home
Start Free TrialLog in
Avatar of telecommando
telecommando

asked on

SQL Query to clean up phone numbers

I have a varchar(100) field in SQL Server 2008 with phone numbers.  There is no set format, some are 1231234567, some are (123)123-4567, some are 123.123.4567, and various other iterations.

I need a query to just find the 10 numbers in the field (and ignore spaces and special charcters) and update them like this 123-123-4567.
ASKER CERTIFIED SOLUTION
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You'd need to edit it slightly to return the format you want.

Change this:

return @vchCleanedPhoneNumber

to

return left(@vchCleanedPhoneNumber, 3) + '-' + left(right(@vchCleanedPhoneNumber, 6), 3) + '-' + right(@vchCleanedPhoneNumber, 4)

This will only work if the number is always 10 digits.

Lee