T-SQL reference › REPLACE

REPLACE in SQL Server

REPLACE swaps every copy of a bit of text for something else. It is not a regular expression. For messy phones, nest a few.

Digits-only phone

SELECT customer_id, phone,
       REPLACE(REPLACE(REPLACE(ISNULL(phone, ''), ' ', ''), '-', ''), '(', '') AS phone_flat
FROM Customers;

Still not perfect () and +44). Good enough to find “the same number typed two ways” before you buy a cleaner.

Try it on Harbour & Co

Amy Nair (Finance Lead) needs this now: “Mailing list for the newsletter. Some names have stray spaces and emails have capitals. Clean it, no duplicates.” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets