Validate email address shape
Use a coarse LIKE pattern to catch rows that are not even shaped like an email, and list them for correction.
The task
customers.email came from free-text input and a form bug once stored whole sentences. Full RFC validation is overkill; you just want rows that clearly are not emails.
The snippet
Runs unchanged on current versions of SQL Server, PostgreSQL, MySQL, SQLite and Oracle.
SELECT id, email
FROM customers
WHERE email IS NOT NULL
AND (email NOT LIKE '%_@_%._%'
OR email LIKE '% %');
Как это работает
The LIKE pattern '%_@_%._%' demands at least one character before an @, then at least one character, a dot, and at least one character after that dot, and NOT LIKE keeps the rows failing that. The second condition rejects embedded spaces, the most common free-text accident. It is a shape check, not an address-existence check.
Variations
PostgreSQL: one regex for the whole shape
PostgreSQLSELECT id, email
FROM customers
WHERE email IS NOT NULL
AND email !~ '^[^@[:space:]]+@[^@[:space:]]+\.[^@[:space:]]+$';
Also reject doubled @ signs
SELECT id, email
FROM customers
WHERE email IS NOT NULL
AND (email NOT LIKE '%_@_%._%'
OR email LIKE '%@%@%'
OR email LIKE '% %');
Questions that come up
Why not validate with a full regex?
Email grammar (RFC 5322) is famously permissive (quoted local parts, comments, long TLDs), so strict regexes both explode in complexity and reject real addresses. For storage hygiene the coarse shape test catches the broken rows; deliverability is the mail server's problem, not the database's.
Keep reading
Related on SQLly
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.