Lessons › SQL › Chapter 9: Data Cleaning
SQL lesson
TRIM() / UPPER() / LOWER() for text cleanup
TRIM removes spaces at the start and end of text. UPPER makes it all capitals, and LOWER makes it all small letters. INITCAP gives each word a capital first letter, like Ada Obi. That way ' Nike' and 'NIKE' become the same, so they match. INITCAP works in PostgreSQL, but not every database has it.
Try this lesson free →Example
Tidy spaces and capitals
SELECT product_id, TRIM(brand) AS clean_brand
FROM products_raw| product_id | clean_brand |
|---|---|
| P1 | Nike |
| P2 | ADIDAS |
| P3 | zara |
| P4 | Puma |
Step by step
- Removes the spaces at the start and end.
SELECT product_id, TRIM(brand) AS clean_brand FROM products_raw - UPPER: capitals. LOWER: small letters. INITCAP: a capital for each word (PostgreSQL only). TRIM inside does both.
SELECT product_id, UPPER(TRIM(brand)) AS clean_brand FROM products_raw
Practice
1. Using products_raw, show product_id and brand with the spaces at the start and end removed. Name it clean_brand.
SELECT product_id, TRIM(brand) AS clean_brand FROM products_raw2. Using suppliers_raw, show supplier_id and country with the spaces at the start and end removed. Name it clean_country.
SELECT supplier_id, TRIM(country) AS clean_country FROM suppliers_rawLearn 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 →