How to Use REPLACE in SQL
Use REPLACE() when text values need one string fragment swapped for another inside query results. This is perfect for cleanup, formatting, standardization, and export-safe transformations.
What you’ll build or solve
You’ll learn how to use REPLACE in SQL to swap text inside strings. You’ll also know when to use it in SELECT output versus permanent updates.
Learn SQL on Mimo
When this approach works best
This approach is the right choice when repeated text fragments must be changed consistently.
Common real-world scenarios include:
- Removing spaces
- Rewriting domains
- Standardizing labels
- Cleaning imported SKUs
- Reformatting phone numbers
This is a bad idea when the replacement should affect only part of a structured format better handled with parsing logic.
Prerequisites
You only need:
- A SQL table with text columns
- Basic
SELECTandUPDATEknowledge
Step-by-step instructions
Step 1: Pass the column, target text, and replacement
The common pattern is:
SQL
REPLACE(column, target, replacement)
Example:
SQL
SELECT REPLACE(phone_number, '-', '') AS clean_phone
FROM customers;
This removes all dashes.
You can replace one domain with another.
SQL
SELECT REPLACE(email, '@old.com', '@new.com') AS updated_email
FROM users;
This changes only the matching text fragment.
What to look for:
- Replaces every matching occurrence
- Great for cleanup and formatting
- Works inside
SELECTandUPDATE - Case sensitivity depends on database collation
- Use carefully for permanent updates
Examples you can copy
Remove spaces
SQL
SELECT REPLACE(full_name, ' ', '') AS compact_name
FROM users;
Fix domain
SQL
SELECT REPLACE(email, '@legacy.com', '@company.com')
FROM users;
SKU cleanup
SQL
SELECT REPLACE(sku, 'OLD-', 'NEW-')
FROM products;
Common mistakes and how to fix them
Mistake 1: Expecting only the first match to change
What the reader might do:
SQL
REPLACE(notes, 'error', 'warning')
Why it breaks: every matching occurrence is replaced.
Corrected approach:
Use parsing logic if only one position should change.
Mistake 2: Using it in UPDATE without previewing
What the reader might do:
SQL
UPDATE users
SET email = REPLACE(email, '@old.com', '@new.com');
Why it breaks: a broad pattern can rewrite too many rows.
Corrected approach:
Preview with SELECT first and use WHERE.
Mistake 3: Replacing ambiguous fragments
What the reader might do:
SQL
REPLACE(product_name, 'Pro', 'Basic')
Why it breaks: this may change unintended words.
Corrected approach:
Use more specific fragments.
Troubleshooting
If too much text changes, make the target fragment more specific.
If nothing changes, confirm exact casing and collation behavior.
If updating permanent data, preview with SELECT.
If only one occurrence should change, use parsing helpers instead.
Quick recap
- Use
REPLACE()to swap text fragments - Every matching occurrence changes
- Great for cleanup and standardization
- Preview before permanent updates
- Use
WHEREin updates for safety
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot