Lessons › SQL › Deep Dive: Data Cleaning
SQL lesson
REPLACE() for fixing inconsistent values
REPLACE() scans every value in a column, finds a specific piece of text, and swaps it out with whatever you want - including nothing, if you just want it gone. Like using find-and-replace in a word processor, but it runs across thousands of rows in an instant.
Try this lesson free →Example
Fixing Retail Text Values with REPLACE()
SELECT product_id, REPLACE(brand, '/US', '') AS brand_clean, category, price
FROM products| product_id | brand_clean | category | price |
|---|---|---|---|
| P1001 | Nike | Fashion | 89.99 |
| P1002 | Adidas | Fashion | 74.5 |
| P1003 | L'Oreal | Beauty | 22.0 |
| P1004 | Samsung | Electronics | 499.99 |
| P1005 | Levi's | Clothing | 59.95 |
Step by step
- Run the query without REPLACE so you can see what the unfiltered values look like.
SELECT product_id, brand, category, price FROM products; - REPLACE finds a substring and substitutes it with another.
SELECT product_id, REPLACE(brand, '/US', ''), category, price FROM products; - AS gives the REPLACE result a friendly column name so the output reads cleanly.
SELECT product_id, REPLACE(brand, '/US', '') AS brand_clean, category, price FROM products;
Practice
1. Using customers, write a query to replace 'Unknown' with 'Other' in the full_name column.
SELECT REPLACE(full_name, 'Unknown', 'Other') FROM customers2. Using products, write a query to replace '-' with 'Other' in the product_id column.
SELECT REPLACE(product_id, '-', 'Other') FROM products3. Using suppliers, write a query to replace 'TBD' with 'Other' in the supplier_name column.
SELECT REPLACE(supplier_name, 'TBD', 'Other') FROM suppliers4. Using employees, write a query to replace 'TBD' with 'Other' in the department column.
SELECT REPLACE(department, 'TBD', 'Other') FROM employees5. Using reviews, write a query to replace '-' with 'Other' in the review_id column.
SELECT REPLACE(review_id, '-', 'Other') FROM reviewsLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →