Skip to content
Cheat sheets/SQL data cleaning

SQL data cleaning

Turn messy rows into trustworthy data — before it ever reaches a dashboard or a model.

Functions for tidying messy datasets

10 MIN31 ENTRIES5 GROUPSUPDATED 2026-11-18

What you'll take away

Standardise text, types and dates in a single pass

Handle NULLs and bad values without breaking queries

Deduplicate records and keep the newest version

31 entries · click a command to copy

01

String operations

Remove whitespace from both ends, or only one side.

Normalise text casing before comparisons or grouping.

Substitute a substring throughout a value.

e.g. REPLACE(phone, '-', '')

Measure string length for validation rules.

Extract a fixed number of characters from either end.

Extract a portion of a string by position.

Clean text using a regular expression.

Join values into one string.

02

NULL and value handling

Return the first non-NULL value — the standard way to replace NULLs.

Return NULL when the column equals a given value, useful to avoid division by zero.

Exclude rows with missing values from a result.

Convert a value to another data type.

Convert safely, returning NULL instead of failing on bad data.

Tip: Great for staging tables with inconsistent inputs.

Apply conditional logic row by row.

03

Numeric operations

Round a number to a chosen precision.

Round down or up to the nearest integer.

Return the absolute value.

Return the remainder — useful for bucketing and parity checks.

Pick the largest or smallest of several values.

04

Dates and times

Pull a single component such as year, month or day.

Add an interval to a date. Some engines use DATE_ADD.

Measure the difference between two dates.

Parse a string into a date value.

Render a date as a formatted string.

Round a timestamp down to a period boundary.

05

Aggregation and deduplication

Collapse rows into groups for aggregate functions.

Filter groups after aggregation — WHERE filters rows before it.

Core aggregate measures for a group.

Find extremes within a group.

Remove duplicate rows from a result set.

Rank rows within a group so you can keep the newest record.

e.g. WITH ranked AS (...) SELECT * FROM ranked WHERE rn = 1

§ CREDITSource and curation

This sheet was curated from reference material in the Skyware AI technical library (SQL data cleaning cheatsheet (Gina Acosta)). Command syntax is factual; explanations, grouping and examples were written by our engineering team. Always verify syntax against your tool or database version.