Learn SQL/Intermediate/String Functions/SQL Cleaning Data with String Functions

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

BUSINESS QUESTION

Marketing wants a normalized lead source beside the original value for QA.

idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
EXAMPLE QUERY
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;
RESULT — exact output from the displayed Queryflo rows
lead_idraw_sourcenormalized_source
301google_adsgoogle_ads
302google_adsgoogle_ads
303emailemail
304linkedinlinkedin

Keeping raw and normalized values side by side makes the real cleanup auditable.

Now You Try

Practice this concept

Marketing wants raw lead sources beside a normalized lowercase value with clean whitespace.

Available schema
marketing

Prefix tables with marketing.table_name.

lead_idraw_sourcenormalized_source
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario