sqlcmd.net validated sql reference
intermediate updating MySQL MariaDB SQL Server PostgreSQL SQLite

Convert Blank Strings To NULL

Normalize empty or whitespace-only text values to `NULL` with `NULLIF(TRIM(...), '')`.

Docker-validated Not currently validation-green

Clean blank email values

Ada's email keeps its meaningful value but loses the surrounding spaces. Bob's empty string and Cara's spaces become NULL after trimming. Dan was already NULL, and TRIM(NULL) remains NULL.

Rows loaded before the example query runs.
Setup
CREATE TABLE contacts (id INT, name VARCHAR(50), email VARCHAR(100));

INSERT INTO
  contacts
VALUES
  (1, 'Ada', ' [email protected] '),
  (2, 'Bob', ''),
  (3, 'Cara', '   '),
  (4, 'Dan', NULL);
Shared across supported engines.
SQL
UPDATE contacts
SET
  email = NULLIF(TRIM(email), '');

SELECT
  id,
  name,
  email
FROM
  contacts
ORDER BY
  id;
Returned rows for the shared example.
idnameemail
1Ada[email protected]
2BobNULL
3CaraNULL
4DanNULL

Supported engines share the same `UPDATE`, `TRIM`, and `NULLIF` expression here.

Where this command helps.

  • cleaning imported contact fields that contain empty strings instead of nulls
  • normalizing optional text columns before adding data quality checks

What the command is doing.

Blank strings and NULL often mean the same thing to users but behave differently in SQL. A cleanup update can trim accidental spaces and convert empty results to NULL, giving downstream queries one consistent missing-value representation. The NULLIF(TRIM(col), '') expression keeps meaningful text, removes edge whitespace, and turns blank values into NULL.