Explain Codes LogoExplain Codes Logo

How to check for null/empty/whitespace values with a single test?

sql
database-specific-functions
query-performance
best-practices
Nikita BarsukovbyNikita Barsukov·Feb 15, 2025
TLDR

Looking to filter NULL, pure whitespace, or empty strings at warp speed? Use:

WHERE COALESCE(NULLIF(TRIM([Col]), ''), '👻') != '👻' -- Even Ghost characters don't escape this!

This lean, mean filtering machine uses TRIM to clean up spaces, NULLIF to convert any emptiness to NULL, and finally COALESCE ensures no useful NULL values bites the dust. Voila!

The right tool for the right DBMS

Everyone loves a great toolbox, so let's make sure we're using the right tools for our database systems:

SQL Server: Meet NULLIF and friends

To efficiently filter out ghost values (you know, those spooky null, empty strings, or whitespace values) in a SQL Server environment, use:

WHERE NULLIF([Col], '') IS NOT NULL -- Ghostbusters SQL Edition `` In the absence of `TRIM`, unifying `LTRIM` and `RTRIM` would be equally exorcising! ### Oracle: We have an NVL for that In Oracle, `NVL` takes over from `ISNULL` to handle nullity. We also put an **index on `TRIM(Col)`** and make efficiency our best friend: ```sql WHERE NVL(TRIM(Col), 'white-noise') != 'white-noise' -- Even white noise can't hide from us!

Just remember, this superpower might drain performance if indexes are ignored.

PostgreSQL: where BTRIM shines

In PostgreSQL, you combine NULLIF with BTRIM (equivalent of TRIM) as follows:

WHERE NULLIF(BTRIM(Col), '') IS NOT NULL -- BTRIM to the rescue!

The key to success lies in balancing all cases and performance.

How to better query performance

Let's rev up that query performance when checking for pesky whitespace, null, or empty strings:

  1. Creating function-based indexes: If a query is like your favorite song on repeat, create a function-based index on TRIM([column_name]).

  2. Avoid full table scans: Be selective with conditions to avoid table scans where function-based indexes are not feasible.

  3. Database-specific functions: Tailoring your approach based on the database function support gives you the upperhand.

Make your SQL select statements bullet-proof

WHERE NOT ((Col IS NULL) OR (TRIM(Col) LIKE '')) -- SQL Sherlock seeks only valid strings

This little nugget keeps rows where Col is not NULL nor has been on a TRIM diet down to nothing but whitespace.

It's all about appropriate usage and knowing the tools you have, RDBMS specific functions included. Check, check, and triple check before sending that query on its mission!

Grappling with "emptylike" conditions

The art of using conditional operators

With the right DBMS support, additional flexibility with operators like CASE or IIF can untangle more complex conditions.

Checking for database support

Those database documentation pages aren't just light bedtime reading — they really do help with function support and optimizing indexing.

Precision in selecting non-empty rows

Because sometimes, you just want a straight shooter:

WHERE ((Col IS NOT NULL) AND (TRIM(Col) != '')) -- The No-Nonsense filter!

Cherry-picks only the rows with meaningful data.