How do you rewrite expressions containing the standard IS DISTINCT FROM
and IS NOT DISTINCT FROM
operators in the SQL implementation in Microsoft SQL Server 2008R2 that does not support them?
The IS DISTINCT FROM
predicate was introduced as feature T151 of SQL:1999, and its readable negation, IS NOT DISTINCT FROM
, was added as feature T152 of SQL:2003. The purpose of these predicates is to guarantee that the result of comparing two values is either True or False, never Unknown.
These predicates work with any comparable type (including rows, arrays and multisets) making it rather complicated to emulate them exactly. However, SQL Server doesn't support most of these types, so we can get pretty far by checking for null arguments/operands:
a IS DISTINCT FROM b
can be rewritten as:((a <> b OR a IS NULL OR b IS NULL) AND NOT (a IS NULL AND b IS NULL))
a IS NOT DISTINCT FROM b
can be rewritten as:(NOT (a <> b OR a IS NULL OR b IS NULL) OR (a IS NULL AND b IS NULL))
Your own answer is incorrect as it fails to consider that FALSE OR NULL
evaluates to Unknown. For example, NULL IS DISTINCT FROM NULL
should evaluate to False. Similarly, 1 IS NOT DISTINCT FROM NULL
should evaluate to False. In both cases, your expressions yield Unknown.
a IS NOT DISTINCT FROM b
as a = b OR a IS NULL and b IS NULL
? Seems much more concise that way. –
Pointsman a = b
evaluates to null which then causes the entire expression to evaluate to null. –
Oxidase IS DISTINCT FROM
implemented in sql server 2019? It is not implemented in sql server 2017. –
Springfield IS DISTINCT FROM
- nor does it have any way to succinctly perform NULL-safe comparisons (other than using set-operations in predicates, which is impractical for scalar comparisons). –
Rasp OR
is not infected by Unknown and you even take advantage of that in your expression. For now I am continuing to use (a IS NULL AND b IS NULL OR a = b)
for a IS NOT DISTINCT FROM b
. –
Varia NOT
(negation), I cannot in SQL Server. The lack of T031 boolean type makes it difficult to use boolean expressions. –
Oxidase NOT
is the one example that I have encountered before but could not remember. Being able to NOT
the expression blindly does matter to me, so I might need to use this pattern or one of the set-based options in the future. –
Varia Another solution I like leverages the true two-value boolean result of EXISTS combined with INTERSECT. This solution should work in SQL Server 2005+.
a IS NOT DISTINCT FROM b
can be written as:EXISTS(SELECT a INTERSECT SELECT b)
As documented, INTERSECT treats two NULL values as equal, so if both are NULL, then INTERSECT results in a single row, thus EXISTS yields true.
a IS DISTINCT FROM b
can be written as:NOT EXISTS(SELECT a INTERSECT SELECT b)
This approach is much more concise if you have multiple nullable columns you need to compare in two tables. For example, to return rows in TableB that have different values for Col1, Col2, or Col3 than TableA, the following can be used:
SELECT *
FROM TableA A
INNER JOIN TableB B ON A.PK = B.PK
WHERE NOT EXISTS(
SELECT A.Col1, A.Col2, A.Col3
INTERSECT
SELECT B.Col1, B.Col2, B.Col3);
Paul White explains this workaround in more detail: https://sql.kiwi/2011/06/undocumented-query-plans-equality-comparisons.html
a
and b
. For nondeterministic expressions a
and b
, or expressions with side-effects (such as logging), that would be very useful. Your second example also emulates (A.Col1, A.Col2, A.Col3) IS DISTINCT FROM (B.Col1, B.Col2, B.Col3)
, which is only supported natively by PostgreSQL (to my knowledge). A very useful predicate, at times. –
Suicidal exists(...intersect...)
idea. Useful when a and b are long expressions. –
Christoper INTERSECT
results in a much faster query. Thanks for sharing! –
Pointsman If your SQL implementation does not implement the SQL standard IS DISTINCT FROM
and IS NOT DISTINCT FROM
operators, you can rewrite expressions containing them using the following equivalencies:
In general:
a IS DISTINCT FROM b <==>
(
((a) IS NULL AND (b) IS NOT NULL)
OR
((a) IS NOT NULL AND (b) IS NULL)
OR
((a) <> (b))
)
a IS NOT DISTINCT FROM b <==>
(
((a) IS NULL AND (b) IS NULL)
OR
((a) = (b))
)
This answer is incorrect when used in a context where the difference between UNKNOWN and FALSE matters. I think that is uncommon, though. See the accepted answer by @ChrisBandy.
If a placeholder value can be identified that does not actually occur in the data, then COALESCE
is an alternative:
a IS DISTINCT FROM b <==> COALESCE(a, placeholder) <> COALESCE(b, placeholder)
a IS NOT DISTINCT FROM b <==> COALESCE(a, placeholder) = COALESCE(b, placeholder)
(a IS DISTINCT FROM b) AND something
, the distinction between UNKNOWN
and FALSE
is essential. If a
and b
are both NULL
, then your emulation will generate NULL
, regardless if something
is TRUE
or FALSE
. –
Suicidal coalesce(a = b, a is null and b is null)
to test if they're the same, thus (a IS NOT DISTINCT FROM b) –
Finable UNKNOWN AND FALSE
evaluates to FALSE
, and UNKNOWN AND TRUE
evaluates to UNKNOWN
. –
Selfexpression TRUE
or FALSE
, never NULL
–
Suicidal Just to extend John Keller's answer. I prefer to use EXISTS
and EXCEPT
pattern:
a IS DISTINCT FROM b
<=>
EXISTS (SELECT a EXCEPT SELECT b)
-- NOT EXISTS (SELECT a INTERSECT SELECT b)
and
a IS NOT DISTINCT FROM b
<=>
NOT EXISTS (SELECT a EXCEPT SELECT b)
-- EXISTS (SELECT a INTERSECT SELECT b)
for one particular reason. NOT
is aligned whereas with INTERSECT
it is inverted.
SELECT 1 AS PK, 21 AS c, NULL AS b
INTO tab1;
SELECT 1 AS PK, 21 AS c, 2 AS b
INTO tab2;
SELECT *
FROM tab1 A
JOIN tab2 B ON A.PK = B.PK
WHERE EXISTS(SELECT A.c, A.B
EXCEPT
SELECT B.c, B.b);
One caveat in rewriting IS DISTINCT FROM and IS NOT DISTINCT FROM would be to not interfere with using indexes, at least when using SQL Server. In other words, when using the following:
WHERE COALESCE(@input, x) = COALESCE(column, x)
SQL Server won't be able to use any index that includes column. So in a WHERE clause, it would be preferable to use the form
WHERE @input = column OR (@input IS NULL AND column IS NULL)
to take advantage of any indexes for column. (Parens only used for clarity)
We are happy to announce that IS [NOT] DISTINCT FROM is now supported as of SQL Server 2022 CTP 2.1 (and the cloud versions as well). So, hopefully the workarounds are no longer needed generally, though they will still work. Documentation Page Link
Spelling it out using CASE
For the reference, the most canonical (and readable) implementation of IS [ NOT ] DISTINCT FROM
would be a well-formatted CASE
expression. For IS DISTINCT FROM
:
CASE WHEN [a] IS NULL AND [b] IS NULL THEN 0 -- FALSE
WHEN [a] IS NULL AND [b] IS NOT NULL THEN 1 -- TRUE
WHEN [a] IS NOT NULL AND [b] IS NULL THEN 1 -- TRUE
WHEN [a] = [b] THEN 0 -- FALSE
ELSE 1 -- TRUE
END
Obviously, other solutions (specifically John Keller's, using INTERSECT
) are more concise.
Using DECODE
if available
I know this question is about SQL Server, but for completeness' sake, Db2 and Oracle support a DECODE()
function, in case of which the following can be emulated:
-- a IS DISTINCT FROM b
DECODE(a, b, 1, 0) = 0
-- a IS NOT DISTINCT FROM b
DECODE(a, b, 1, 0) = 1
FALSE
and TRUE
are not constants in SQL Server. You pulled an example for MySQL. –
Varia IS DISTINCT FROM
. The tag sql is probably there just because that is the broader topic. Three of the tags are about SQL Server: tsql, sql-server, and sql-server-2008-r2. The question itself says “such as”, unfortunately, but it also specifies an exact SQL Server version and that is how everyone else interpreted it. –
Varia These expressions can be a good substitute for the IS DISTINCT FROM logic and perform better than the previous examples because they end up being compiled by SQL server into a single predicate expression which will result in approx. half the operator cost on a filter expression. They are essentially the same as the solutions as provided by Chris Bandy, however they use nested ISNULL and NULLIF functions to perform the underlying comparisons.
(... obviously ISNULL could be substituted with COALESCE if you prefer)
a IS DISTINCT FROM b
can be rewritten as:ISNULL(NULLIF(a, b), NULLIF(b, a)) IS NOT NULL
a IS NOT DISTINCT FROM b
can be rewritten as:ISNULL(NULLIF(a, b), NULLIF(b, a)) IS NULL
a IS NOT DISTINCT FROM b
can be rewritten as:
(a IS NOT NULL AND b IS NOT NULL AND a=b) OR (a IS NULL AND b is NULL)
a IS DISTINCT FROM b
can be rewritten as:
NOT (a IS NOT DISTINCT FROM b)
This is an old question and there is a new answer. It is easier to understand and maintain.
-- a IS DISTINCT FROM b
CASE WHEN (a = b) OR (a IS NULL AND b IS NULL) THEN 1 ELSE 0 END = 0
-- a IS NOT DISTINCT FROM b
CASE WHEN (a = b) OR (a IS NULL AND b IS NULL) THEN 1 ELSE 0 END = 1
It should be noted that this syntax alternative to IS [NOT] DISTINCT FROM
works in all major SQL databases (see link at the end). This and the alternatives are elaborately explained here
© 2022 - 2024 — McMap. All rights reserved.