runtime
33 / 88

lesson 33 Dates, strings & subqueries · intermediate

Clean strings & cast values

Clean text and convert values to the types you need.

the idea

TRIM removes outside spaces, LOWER normalizes case, SUBSTR extracts a portion, and CAST makes a conversion explicit. Keep NULL visible when the source is missing. SQLite uses SUBSTR; many engines also provide SUBSTRING.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

members5 columns · 8 rows
idinteger · keyemailtextcountrytextjoined_attextreferrer_idinteger
1 ALICE@Example.com US2025-01-01NULL
2ben@example.comUS2025-01-021
3 Cara@example.comGB2025-01-031
4dev@example.com IN2025-01-042
5NULLCA2025-01-05NULL

showing 5 of 8 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT id,LOWER(TRIM(email)) AS clean_email
FROM members;

Watch for: Normalizing email is useful for analysis, but changing a real identity field needs business rules.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. See messy emails before cleaning. Whitespace and mixed case make the same address look different. Cleaning must happen before you compare or group.

    step-1.sql
    SELECT id, email
    FROM members
    ORDER BY id
    LIMIT 8;
  2. Normalize with TRIM and LOWER. TRIM removes surrounding spaces; LOWER folds case. Together they produce a stable clean_email.

    step-2.sql
    SELECT id, LOWER(TRIM(email)) AS clean_email
    FROM members;

challenge 1

Return id and clean_email for every member: trim outside spaces and lowercase the email; preserve NULL.

Return idclean_emailany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.