Explain Codes LogoExplain Codes Logo

How to Check for is Not Null and is Not Empty String in SQL Server?

sql
best-practices
performance
data-variants
Nikita BarsukovbyNikita Barsukov·Dec 21, 2024
TLDR
WHERE column_name > '' /*Pro Tip: NULL or '' doesn't lift this ☝️*/

This elegant line screens NULL and empty strings, as neither NULL nor '' meet the > clause.

Interpreting empty strings

Empty strings and strings entirely composed of spaces are not the same species in the SQL Server realm. column_name > '' filters out empty strings but tolerates strings of whitespace. Handle this by integrating DATALENGTH:

WHERE column_name <> '' AND column_name IS NOT NULL AND DATALENGTH(column_name) > 0 /*Extra layer, extra protection*/

Eliminating concealed spaces

If you've got a keen eye for unwanted spaces, the LTRIM and RTRIM functions will be your secret agents:

WHERE LTRIM(RTRIM(column_name)) <> '' AND column_name IS NOT NULL /*Hide & seek with spaces*/

Null behaving weird

An interesting maneuver, NULLIF(column_name, ''), converts an empty string to a NULL. We pair this with IS NOT NULL to solicit rows with valid data:

WHERE NULLIF(column_name, '') IS NOT NULL /*It's like a magic trick*/

Dealing with nulls and empty strings

The COALESCE function offers a workaround to possible nulls by assigning a default value:

WHERE COALESCE(column_name, 'a non-empty default value') <> 'a non-empty default value' /*Again with the tricks*/

Here we're assuming that 'a non-empty default value' is not an authentic value in your data.

Contemplating performance

Always a wise decision to size up performance. Use of facilities like LEN, DATALENGTH, LTRIM, and RTRIM may rope in performance implications when dealing with mammoth datasets.

Anticipated quirks and precautions

Data variations denote handling of potential edge cases:

  • Single-character fields: Functional check when the permitted field's length is of maximum 1.
  • Character encoding: DATALENGTH is susceptible to encoding influencing byte count.
  • Test everything: Deploying queries sans testing is a big no. Leverage platforms like SQL Fiddle to validate your SQL.

Making the right choice

Recap the determinants such as dataset, performance consideration, specific edge cases while choosing the efficient pathway:

  • Simplicity: column_name > '' is swift and handy.
  • Strictness: LTRIM, RTRIM come to the rescue when spaces need ousting.
  • Non-text data: DATALENGTH trumps LEN when wrestling with binary data.