Stop 04 · Functions & ExpressionsTopics · 2/8

Cleaning Text (TRIM, REPLACE, LOWER)

Part of the SQL Roadmap

In short

"Mumbai", " mumbai" and "MUMBAI " look the same to you, but SQL sees three different cities. Before you group or match text, you need to clean it.

Real text data has extra spaces, mixed case and stray characters. TRIM removes leading and trailing spaces, REPLACE swaps characters and LOWER or UPPER standardise case so values group and match correctly.

The jaadu rinses muddy carrots and spinach under a kitchen tap and lines up the clean ones neatly on a cutting board.

Think of it like this

Washing sabzi before cooking

Vegetables from the market come with mud, loose leaves and odd bits. You rinse them, trim the ends and cut them evenly before they go in the kadhai. TRIM, REPLACE and LOWER are that washing step, so every value is tidy before you count or join on it.

Watch it happen

Cleaning one messy city value

Raw
TRIM
REPLACE
LOWER
Clean

1/5The raw value is ' Navi-Mumbai ' with spaces at both ends.

The big ideas

What you need to know

TRIM removes edge spaces

TRIM(city) removes spaces at both ends. LTRIM and RTRIM do one side. Spaces in the middle stay.

REPLACE swaps every match

REPLACE(phone, ' ', '') removes every space. REPLACE(phone, '+91', '') drops the country code.

LOWER or UPPER for matching

Pick one case for comparing and grouping. Show a nicely cased version with INITCAP in PostgreSQL.

Nest them

LOWER(TRIM(city)) is a common combo. Work from the inside out when you read it.

See it in code

Walk through the code

sql
1-- Before cleaning: the same city counted three times2SELECT city, COUNT(*) FROM customers GROUP BY city;3 4-- After cleaning5SELECT6  LOWER(TRIM(city))  AS clean_city,7  COUNT(*)           AS customers8FROM customers9GROUP BY LOWER(TRIM(city))10ORDER BY customers DESC;11 12-- Normalise phone numbers like '+91 98765 43210'13SELECT REPLACE(REPLACE(phone, '+91', ''), ' ', '') AS phone_1014FROM customers;

1/3Mumbai, ' mumbai' and 'MUMBAI ' each get their own row.

TRIM, REPLACE and LOWER work the same in PostgreSQL, MySQL and SQL Server. For messier patterns PostgreSQL has REGEXP_REPLACE, and MySQL 8 does too.

Save yourself the pain

Common mistakes

Oops

Expecting TRIM to fix double spaces in the middle, like 'New Delhi'.

Do this instead

TRIM only touches the ends. Use REGEXP_REPLACE(city, '\s+', ' ', 'g') in PostgreSQL.

Oops

Cleaning in SELECT but grouping by the raw column.

Do this instead

Group by the same cleaned expression, or the duplicates come back.

Oops

Cleaning text in every report query forever.

Do this instead

Clean once with an UPDATE or in the data pipeline, so every query starts with tidy data.

Your mission

Tidy a messy column

~25 min

Quick check

What does TRIM(' Navi Mumbai ') return?

Go deeper

  1. 1Find city names that differ only by case or extra spaces, then fix them in a SELECT.
  2. 2Remove dashes and spaces from phone numbers with REPLACE.
  3. 3Compare COUNT(DISTINCT city) before and after cleaning.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge