Data cleansing
Data cleansing (or data cleaning) is the process of detecting and correcting inaccurate, incomplete, inconsistent, or malformed data so it's reliable for analysis and downstream systems.
Overview
Data cleansing covers the concrete fixes applied once data profiling has revealed problems: filling or removing nulls, standardizing formats (dates, phone numbers, casing), trimming whitespace, fixing encoding issues, correcting invalid values, and reconciling inconsistent representations of the same thing (e.g. "NY", "N.Y.", and "New York" all meaning the same state).
It sits between raw ingestion and modeling in most pipelines. Cleansing logic is typically expressed as SQL transformations, dbt models, or scripts that run as part of a scheduled job, so the same fixes are applied consistently every time new data arrives rather than being patched ad hoc.
Common cleansing operations in SQL
Handling nulls and defaults with COALESCE:
Copy code
SELECT
customer_id,
COALESCE(country, 'UNKNOWN') AS country,
COALESCE(discount_pct, 0) AS discount_pct
FROM raw_customers;
Trimming and standardizing text:
Copy code
SELECT
TRIM(email) AS email,
UPPER(TRIM(state_code)) AS state_code
FROM raw_customers;
Pattern-based cleanup with regular expressions. DuckDB's regexp_replace takes a string, a pattern, a replacement, and optional flags:
Copy code
-- strip non-digit characters from a phone number
SELECT regexp_replace(phone, '[^0-9]', '', 'g') AS phone_digits
FROM raw_customers;
Standardizing categorical values with CASE:
Copy code
SELECT
CASE
WHEN LOWER(status) IN ('active', 'a') THEN 'ACTIVE'
WHEN LOWER(status) IN ('cancelled', 'canceled', 'c') THEN 'CANCELLED'
ELSE 'UNKNOWN'
END AS status_clean
FROM raw_subscriptions;
Type casting and validation:
Copy code
SELECT
TRY_CAST(order_date AS DATE) AS order_date,
TRY_CAST(amount AS DECIMAL(10,2)) AS amount
FROM raw_orders;
TRY_CAST returns NULL instead of erroring on a value that can't be converted, which is useful for isolating bad rows rather than failing an entire load.
Why it matters
Uncleaned data quietly breaks joins, skews aggregates, and undermines trust in dashboards and models. Cleansing rules, once written, are usually one of the first transformation steps in a pipeline (often the first dbt model layer, sometimes called "staging") so that every downstream model works from a consistent, validated base rather than re-deriving the same fixes repeatedly.
Related terms
TRY_CAST attempts to convert a value to another data type and returns NULL instead of raising an error if the conversion fails.
Data profiling →Data profiling is the process of examining a dataset to understand its structure, content, and quality — things like data types, value distributions, null rates, and cardinality — before using it for analysis or building pipelines on top of it.
Data transformation →Data transformation is the process of converting data from its raw, source format into a structured form suited to analysis — through operations like cleaning, joining, aggregating, and reshaping.
String functions →String functions manipulate text values in SQL — concatenating, trimming, searching, splitting, and reformatting strings as part of a query.
Data wrangling →Data wrangling is the broader process of taking raw, messy data and manually or programmatically converting it into a structured, usable format for analysis — encompassing cleaning, reshaping, joining, and enriching.
Regular expressions in SQL →SQL engines support regular expressions through functions and operators like REGEXP_MATCHES, REGEXP_REPLACE, and SIMILAR TO for pattern-based text matching and extraction.
FAQS
Data cleansing fixes correctness and consistency problems in existing data (nulls, typos, formatting). Data transformation reshapes or combines data for a new purpose (aggregating, joining, pivoting). Cleansing is often the first stage of a broader transformation pipeline.
Common approaches are to route them to a default or UNKNOWN value with COALESCE/CASE, use TRY_CAST to convert invalid values to NULL rather than erroring, or quarantine the offending rows in a separate table for manual review rather than silently dropping them.
