Example 1
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.
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);UPDATE contacts
SET
email = NULLIF(TRIM(email), '');
SELECT
id,
name,
email
FROM
contacts
ORDER BY
id;| id | name | |
|---|---|---|
| 1 | Ada | [email protected] |
| 2 | Bob | NULL |
| 3 | Cara | NULL |
| 4 | Dan | NULL |
Supported engines share the same `UPDATE`, `TRIM`, and `NULLIF` expression here.