SQL Cleaning Data with String Functions
A closing, end-to-end example — every function from this series combined into one query that fully cleans a messy row.
What & Why
Real category fields can differ only because of casing or extra whitespace. Queryflo keeps the original marketing.leads.source beside a normalized value so every cleanup decision remains auditable.
The worked pipeline applies TRIM, collapses repeated whitespace with REGEXP_REPLACE, and then applies LOWER. It standardizes presentation without overwriting the stored source value.
See How It Works
Marketing wants a normalized lead source beside the original value for QA.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
SELECT
id AS lead_id,
source AS raw_source,
LOWER(REGEXP_REPLACE(TRIM(source), '\s+', ' ', 'g')) AS normalized_source
FROM marketing.leads
WHERE source IS NOT NULL
ORDER BY lead_id
LIMIT 20;| lead_id | raw_source | normalized_source |
|---|---|---|
| 301 | google_ads | google_ads |
| 302 | google_ads | google_ads |
| 303 | ||
| 304 |
Keeping raw and normalized values side by side makes the real cleanup auditable.
Practice this concept
Marketing wants raw lead sources beside a normalized lowercase value with clean whitespace.
marketingPrefix tables with marketing.table_name.
lead_idraw_sourcenormalized_sourcemarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
Sign up free to try it on a real business scenario