I'm pretty sure this is a implicit conversion but I wasn't able to find information about it.
Suppose I have this SQL:
SELECT
CASE WHEN (SELECT 1 WHERE (1=1)) = 1 THEN 1 ELSE 0 END
My subquery is returning one row and hence compared to 1. This leads my CASE WHEN to produce the 1 output.
Even if I set the subquery to return no rows like the sample below, the CASE statement returns the 0 result:
SELECT
CASE WHEN (SELECT 1 WHERE (1=0)) = 1 THEN 1 ELSE 0 END
- Does the SQL interpreter make a conversion between "empty set" to 0 or 1 ?
-
I don't feel comfortable using this type of comparison. In my humble opinion, it's better to use EXISTS like this:
SELECT CASE WHEN EXISTS(SELECT 1 WHERE (1=1)) THEN 1 ELSE 0 END
Is there any advantage or disadvantage if I don't use EXISTS ?
Best Answer
No, The query didn't return 0 because the empty result is considered equal to 0 but because the empty result set is considered
NULL
and null isn't equal to anything.As you found out
Returns 0
But
Also returns 0.
So the empty result set is not 1 and not 0, it's NULL as evidenced by
which returns 1.
See this dbfiddle with that code.
NULL
is unknown so SQL Server doesn't know what it's equal to.If you rely on the existence of a result set, I agree
EXISTS
is probably the way to go. That's what the construction is made for.If you want to compare 2 values off course you'll need to use
CASE
but for your stated scenario I'd use EXISTSYour comparison will break if your result set has more than one row or column.
For example
returns
and
returns