SQL Server: Writing CASE expressions properly when NULLs are involved
We’ve all written a CASE expression (yes, it’s an expression and not a statement) or two every now and then. But did you know there are actually 2 formats you can write the CASE expression in? This actually bit me when I was trying to add some new functionality to an old stored procedure. In some rare cases the stored procedure just didn’t work correctly. After a quick look it turned out to be a CASE expression problem when dealing with NULLS.
In the first format we make simple “equals to” comparisons to a value:
SELECT CASE <value>
WHEN <equals this value> THEN <return this>WHEN <equals this value> THEN <return this>
-- ... more WHEN's here
ELSE <return this>
END
Second format is much more flexible since it allows for complex conditions. USE THIS ONE!
SELECT CASE
WHEN <value> <compared to> <value> THEN <return this>
WHEN <value> <compared to> <value> THEN <return this>
-- ... more WHEN's here
ELSE <return this>
END
Now that we know both formats and you know which to use (the second one if that hasn’t been clear enough) here’s an example how the first format WILL make your evaluation logic WRONG.
Run the following code for different values of @i. Just comment out any 2 out of 3 “SELECT @i =” statements.
DECLARE @i INT
SELECT @i = -1 -- first result
SELECT @i = 55 -- second result
SELECT @i = NULL -- third result
SELECT @i AS OriginalValue,-- first CASE format. DON'T USE THIS!
CASE @i
WHEN -1 THEN '-1'
WHEN NULL THEN 'We have a NULL!'
ELSE 'We landed in ELSE'
END AS DontUseThisCaseFormatValue,-- second CASE format. USE THIS!
CASE
WHEN @i = -1 THEN '-1'
WHEN @i IS NULL THEN 'We have a NULL!'
ELSE 'We landed in ELSE'
END AS UseThisCaseFormatValue
When the value of @i is –1 everything works as expected, since both formats go into the –1 WHEN branch.
When the value of @i is 55 everything again works as expected, since both formats go into the ELSE branch.
When the value of @i is NULL the problems become evident. The first format doesn’t go into the WHEN NULL branch because it makes an equality comparison between two NULLs.
Because a NULL is an unknown value: NULL = NULL is false. That is why the first format goes into the ELSE Branch but the second format correctly handles the proper IS NULL comparison.
Please use the second more explicit format. Your future self will be very grateful to you when he doesn’t have to discover these kinds of bugs.
Legacy Comments
John Maher
2013-03-19 |
re: SQL Server: Writing CASE expressions properly when NULLs are involved Very informative. Thanks. |
Matt Velic
2013-03-19 |
re: SQL Server: Writing CASE expressions properly when NULLs are involved It always comes back to NULLs, doesn't it? |
Marcel Miklovic
2013-03-23 |
re: SQL Server: Writing CASE expressions properly when NULLs are involved The first format gets correct result only if you set: SET ANSI_NULLS OFF |
Richard
2013-03-25 |
re: SQL Server: Writing CASE expressions properly when NULLs are involved The "WHEN {value} {compared to} {value} THEN" is slightly misleading - it's actually "WHEN {condition} THEN". The {condition} can be any valid boolean expression. |
David Rymell
2013-03-26 |
re: SQL Server: Writing CASE expressions properly when NULLs are involved Great article! Thanks! |
Marc Jellinek
2013-03-26 |
re: SQL Server: Writing CASE expressions properly when NULLs are involved In regards to format 1 vs. format 2; The danger with format 2 is that every test (WHEN) must be mutually exclusive and it is the programmers responsibility to ensure that they are mutually exclusive. I've run into more than one "bug" where non-exclusive conditions led to inappropriate execution (the first true condition is executed, then the CASE statement exits). You can also use ISNULL() to handle your NULLs. |
B T
2013-03-28 |
re: SQL Server: Writing CASE expressions properly when NULLs are involved The two formats are tools and as stated it is up to the programmer to know which tool to use and how to use it. You don't use a hammer to drive screws do you? SELECT @i AS OriginalValue, CASE ISNULL(@i,0) WHEN -1 THEN '-1' WHEN 0 THEN 'We have a NULL!' ELSE 'We landed in ELSE' END AS DontUseThisCaseFormatValue |