How to COALESCE for empty strings and NULL values?
To make NULL
or empty strings great again (by converting them to something meaningful), use NULLIF
to convert empty strings into NULL
and apply COALESCE
with your fallback value:
If you're wrestling with multiple columns, use NULLIF
within COALESCE
for each of them:
Swift tweaks for swift performance
Now, you may have heard the rumor: that user functions are like molasses in January, while native SQL functions are as fast as roadrunners. It's true—almost. Optimized user functions can give native SQL functions a run for their money.
Let's dive deeper
Deploy COALESCE and NULLIF side by side
Keep NULLIF(column_name, '')
close to your COALESCE
. The duo champions against both trifling NULLs and empty strings:
Make functions fast, not furious
Need to juggle multiple columns and still beat the clock? Custom scalar functions at your rescue:
Be sure to test this function against a Timex (native SQL) to ensure speed.
Don't forget scalability
If you're dealing with variable length lists, opt for a table-valued function or dynamic SQL.
Mind the tricky corner cases
There might be occasions when even empty strings have significance. Thinking about whether you want to treat them differently from NULL
ahead of time can save headaches later.
Was this article helpful?