I need to compare two columns, and am hitting issues when comparing NULL
against an empty string. Put simply, this is what I'm looking at.
DECLARE @EmptyString VARCHAR(20) = '', -- Could have valid VARCHAR data, empty string, or NULL
@Null VARCHAR(20) = Null; -- Could have valid VARCHAR data, empty string, or NULL
SELECT
CASE WHEN @EmptyString != @Null THEN 'Pass' ELSE 'Fail' END AS EmptyStringVsNull
In this case, because we all know that an empty string and a null column value are different, I would hope to see the result come back as 'Pass', however it doesn't.
From Martin's asnwer here, he explains that that a comparison like this results to UNKNOWN
, rather than TRUE
or FALSE
, which clarifies the reason I am seeing these results, but I need find a solution to this. I know there must be a simply way around this that I'm missing...
I know that there are a few built in functions such as ISNULL()
and NULLIF()
, however I don't know if these can help in this situation...
If we use the ISNULL()
function to set null values to an empty string, then the comparison wont work as an empty string is equal to an empty string, for example
DECLARE @EmptyString VARCHAR(20) = '',
@Null VARCHAR(20) = Null;
SELECT
CASE WHEN ISNULL(@EmptyString, '') != ISNULL(@Null, '') THEN 'Pass' ELSE 'Fail' END AS EmptyStringVsNull
This also returns 'FAIL', so this is a no go. I could always use ISNULL
to convert this to a different string, but this still isn't suitable, as the empty string may have a different value which by chance could match whatever we decide to convert null values to.
DECLARE @EmptyString VARCHAR(20) = '',
@Null VARCHAR(20) = Null;
SELECT
CASE WHEN NULLIF(@EmptyString, '') != NULLIF(@Null, '') THEN 'Pass' ELSE 'Fail' END AS EmptyStringVsNull
If we use the NULLIF()
function to convert empty strings to null, our comparison still doesn't return true. This is because, as explained in the linked post, comparing null values results in UNKNOWN
.
With the simple SQL example above, how can I check that a NULL
value is not equal to an empty string?
Due to this structure of the NULL values, it is not possible to use traditional comparison (=, <, > and <>) operators in the queries. As a matter of fact, in the SQL Standards using the WHERE clause as the below will lead to return empty result sets.
An empty string is a string with zero length or no character. Null is absence of data. That is one interpretaion of NULL. In certain contexts, it is clearly the correct one. But SQL is not consistent when it comes to NULL, so there are contexts where NULL means unknown.
Please. There's a nice article here which discusses this point. Key things to take away are that there is no difference in table size, however some users prefer to use an empty string as it can make queries easier as there is not a NULL check to do. You just check if the string is empty.
Any arithmetic expression containing a null always evaluates to null. For example, null added to 10 is null. In fact, all operators (except concatenation) return null when given a null operand. SELECT CASE WHEN REGEXP_REPLACE ('123', ' [0-9]', '', 1) = '' Ok. So, '' (Empty String) is NULL.
Your first example returns fail because you have the wrong operator. If you want to see if something equals something else you use =
, not !=
Here is the code that proves that NULL
can be compared to ''
:
DECLARE @EmptyString VARCHAR(20) = '',
@Null VARCHAR(20) = Null;
SELECT
CASE WHEN ISNULL(@EmptyString, '') = ISNULL(@Null, '')
THEN 'Pass' ELSE 'Fail'
END AS EmptyStringVsNull
It returns pass because you use =
, not !=
Just reverse the comparison:
SELECT (CASE WHEN @EmptyString = @Null THEN 'Fail' ELSE 'Pass' END) as EmptyStringVsNull
The only complication is if you want two NULL
values to be the same. If so:
SELECT (CASE WHEN @EmptyString = @Null OR (@EmptyString IS NULL AND @Null IS NULL)
THEN 'Fail' ELSE 'Pass'
END) as EmptyStringVsNull
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With