Learn SQL

SQL String Functions for Cleaning Text: TRIM, LOWER, SPLIT_PART, and REGEXP_REPLACE

Exported text is messy. A handful of SQL string functions fix most of it: trim and lowercase values, join fields safely, split out parts, and strip characters with regex.

Published 2026-10-29·Updated 2026-10-29

Why text needs cleaning before you analyze it

Text exported from forms, CRMs, and spreadsheets is full of invisible differences: trailing spaces, random capital letters, and phone numbers typed five different ways. To SQL, '[email protected] ' and '[email protected]' are different values, so counts, joins, and duplicate checks go wrong.

The examples use a deliberately messy contacts table from the String Functions chapter in SQL for Files, a browser SQL editor that runs DuckDB locally on CSV, JSON, and Parquet files.

Fix letter case and spaces with LOWER, UPPER, and TRIM

LOWER and UPPER change case; TRIM removes spaces from both ends, and LTRIM or RTRIM from one end. Nest them to normalize a value in one expression.

SELECT id, LOWER(TRIM(email)) AS clean_email
FROM contacts_raw
WHERE email IS NOT NULL
ORDER BY id;

Concatenate safely around NULL

The || operator joins text, but any NULL part turns the whole result into NULL. concat skips NULL values, and concat_ws joins with a separator while skipping NULL values, so optional fields do not leave doubled separators.

SELECT
  id,
  TRIM(full_name) || ' from ' || TRIM(city) AS display_name,
  concat_ws(' | ', TRIM(full_name), email, phone) AS contact_card
FROM contacts_raw
ORDER BY id;

Extract parts with SPLIT_PART, STRPOS, and SUBSTRING

When a value has a clear separator, SPLIT_PART is the most readable way to pull out a piece, such as the domain of an email. STRPOS finds where a substring starts, and SUBSTRING, LEFT, and RIGHT take characters by position. Positions start at 1.

SELECT
  SPLIT_PART(LOWER(TRIM(email)), '@', 2) AS email_domain,
  COUNT(*) AS contacts
FROM contacts_raw
WHERE email IS NOT NULL
GROUP BY email_domain
ORDER BY contacts DESC;

Strip characters with REPLACE and REGEXP_REPLACE

REPLACE swaps one fixed string for another. For character classes, such as everything that is not a digit, use REGEXP_REPLACE with a regular expression.

SELECT id, REGEXP_REPLACE(phone, '[^0-9]', '', 'g') AS phone_digits
FROM contacts_raw
WHERE phone IS NOT NULL
ORDER BY id;

Do not forget the 'g' flag

In DuckDB, REGEXP_REPLACE replaces only the first match unless you pass 'g' as the fourth argument. Without it, only the first non-digit character disappears.

Clean once, then query the clean table

Instead of repeating the same cleanup in every query, create a cleaned copy of the table once and point later queries at it.

CREATE OR REPLACE TABLE contacts_clean AS
SELECT
  id,
  TRIM(full_name) AS full_name,
  LOWER(TRIM(email)) AS email,
  REGEXP_REPLACE(phone, '[^0-9]', '', 'g') AS phone_digits,
  TRIM(city) AS city
FROM contacts_raw;

Practice string functions

  1. Open the String Functions chapter in the SQL for Files Learn SQL course.
  2. Load the contacts_raw sample table.
  3. Solve the four challenges: normalize emails, build display names, extract domains, and clean phone numbers.
  4. Use the same functions on a messy CSV export of your own.

Continue in the editor

Open SQL for Files to add your own CSV, JSON, or Parquet files and try these examples locally in your browser.