BBampaa

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 →
About 5–10 minutes · no sign-up needed

Example

Tidy spaces and capitals

SELECT product_id, TRIM(brand) AS clean_brand
FROM products_raw

Result:

product_idclean_brand
P1Nike
P2ADIDAS
P3zara
P4Puma

Step by step

  1. Removes the spaces at the start and end.
    SELECT product_id, TRIM(brand) AS clean_brand FROM products_raw
  2. 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

Try each one in your head first, then open the answer.

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_raw
2. 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_raw

Learn 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 →
About 5–10 minutes · no sign-up needed