In the real world, data is rarely pristine. It comes with trailing spaces, inconsistent capitalization, strange characters, and missing values.
Before you can analyze data, you must clean it. Here are the 4 essential tools in your SQL cleaning kit.

A Real Business Scenario: Preparing CRM Exports for Reporting
One of the most common cleanup jobs is importing a CSV from a CRM, billing tool, or spreadsheet and discovering that the same logical field appears in five slightly different shapes.
You might see:
- emails with leading or trailing spaces
- statuses that are sometimes blank and sometimes
NULL - phone numbers stored with different separators
- duplicated people whose only difference is capitalization
If you aggregate that data without cleaning it first, the report becomes misleading fast. "Active" and NULL may look like different business states. "[email protected]" and " [email protected] " may behave like different customers. SQL cleaning is not cosmetic. It changes whether your grouped results are trustworthy.
1. COALESCE: The Null-Buster
NULL values can ruin math and look ugly in reports. COALESCE() returns the first non-null value in a list.
SELECT COALESCE(phone_number, 'No Phone') FROM users;
2. TRIM: The Whitespace Whacker
Spaces at the start or end of strings are invisible enemies. They make 'Alice' imply not equal to 'Alice '.
SELECT TRIM(name) FROM users;
3. LOWER / UPPER: The Case Normalizer
'iPad', 'IPad', and 'ipad' are three different strings to a computer. Normalize them to fix grouping.
SELECT LOWER(device_type) FROM devices;
4. REPLACE: The Substitution Surgeon
Great for fixing inconsistent formats, like removing dashes from phone numbers or fixing typos.
SELECT REPLACE(phone, '-', '') FROM users;
A Common Wrong Move: Overwriting Raw Data Too Early
A frequent mistake is applying cleanup functions directly in an UPDATE before you have compared raw values with cleaned values.
UPDATE customers
SET email = LOWER(TRIM(email));
That may be fine eventually, but it is risky as a first step:
- you lose the original messy value that might matter for debugging imports
- you may normalize a field incorrectly and not notice until downstream joins stop matching
- you cannot easily compare "before" and "after" logic once the original input is gone
For exploratory cleanup, start with a SELECT that shows raw and cleaned columns side by side. Promote that logic to an UPDATE only after you understand the impact.
Putting It All Together: Interactive Cleaning
Let's clean a messy customer list.
The Horror Show (Raw Data):
- Inconsistent names (alice, Bob, CHARLIE)
- Messy emails with spaces (
[email protected]) - NULL phone numbers
- NULL status (should be 'Active')
The Cleanup Query:
Tip: Depending on your SQL dialect (PostgreSQL, SQL Server), you might have INITCAP() to capitalize just the first letter (e.g., 'Alice'). Standard SQL doesn't always have INITCAP, so we often settle for UPPER or complex SUBSTRING logic for names.
Boundary and Performance Notes
Cleaning logic can be simple at the row level and still expensive at scale.
- Wrapping indexed columns in functions such as
LOWER(email)can make equality filters and joins less index-friendly unless you also maintain a normalized column or functional index. - Repeated cleanup expressions inside many downstream queries are a sign that the normalized shape should become part of your ingestion pipeline.
- Replacing values blindly can collapse meaningful distinctions. For example, turning both blank strings and
NULLinto the same label may be wrong if one means "missing from source" and the other means "intentionally empty". - Text cleanup rules are often locale-sensitive. Uppercasing names is convenient for grouping, but it may not be the best display format for real customer-facing output.
The right question is not just "How do I clean this text?" but also "At what stage should this normalization happen, and do we need to preserve the raw form too?"
Deduplication
Another huge part of cleaning is removing duplicates.
The best way to deduplicate? Use ROW_NUMBER().
WITH flagged_dupes AS (
SELECT
*,
ROW_NUMBER() OVER(PARTITION BY email ORDER BY id DESC) as rn
FROM users
)
SELECT * FROM flagged_dupes WHERE rn = 1; -- Keep only the latest entry
When NOT to Clean Everything in SQL
SQL is strong at deterministic cleanup close to the data. It is not always the best layer for every transformation.
Examples where SQL may not be the best primary tool:
- complex address parsing that needs specialized libraries or external validation
- fuzzy matching of names where "close enough" is a product decision, not a simple string rule
- one-time ad hoc cleanup where the real fix belongs upstream in the export or form validation layer
If the same dirty pattern keeps reappearing, the durable fix is often to repair the data pipeline or input validation, not just to add more cleanup expressions in downstream analytics.
Official References
- PostgreSQL string functions and operators for vendor documentation on trimming, replacement, casing, and related text operations.
- SQLite core functions for SQLite behavior of functions such as
trim,replace,lower,upper, andcoalesce. - SQL Server
COALESCEdocumentation for another major-engine reference on null-handling semantics.
Tool Workflow
Use tools when cleaned data still needs to move between formats or test environments
Cleaning is rarely the final step. Once the values are normalized, it often helps to convert them into importable SQL, reusable fixtures, or safer non-production datasets.
CSV to SQL
Turn cleaned spreadsheet exports into runnable INSERT statements when the source data still lives in CSV.
JSON to SQL
Useful when the cleaned records came from API payloads or semi-structured exports rather than flat files.
Data Conversion Workflow Hub
Use the broader conversion path when cleaned rows need to move between CSV, JSON, spreadsheets, and SQL.
Mock Data Generator
Generate extra representative rows once you understand the cleaned shape your downstream systems expect.
Related Articles
- Generating Massive Test Data with SQL for building larger synthetic datasets after you understand the cleaned target shape.
- Working with JSON in SQL for cases where the messy fields stay semi-structured instead of being fully flattened.
- SQL Data Types for deciding when cleaned text should become stricter numeric, date, or boolean columns.
Conclusion
Dirty data leads to dirty insights. By mastering TRIM, COALESCE, and normalization functions, you ensure your analysis is built on a solid foundation.