How to rewrite IS DISTINCT FROM and IS NOT DISTINCT FROM in SQL Server 20008R2?

SqlSql ServerTsqlSql Server-2008-R2Ansi Sql

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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.

Solution 2 - Sql

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

Solution 3 - Sql

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)

Solution 4 - Sql

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)

Solution 5 - Sql

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);

DBFiddle Demo

Solution 6 - Sql

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)

Solution 7 - Sql

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.

More details here.

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

Solution 8 - Sql

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

Solution 9 - Sql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason KresowatyView Question on Stackoverflow
Solution 1 - SqlChris BandyView Answer on Stackoverflow
Solution 2 - SqlJohn KellerView Answer on Stackoverflow
Solution 3 - SqlJason KresowatyView Answer on Stackoverflow
Solution 4 - SqlBoydView Answer on Stackoverflow
Solution 5 - SqlLukasz SzozdaView Answer on Stackoverflow
Solution 6 - SqlwojtekView Answer on Stackoverflow
Solution 7 - SqlLukas EderView Answer on Stackoverflow
Solution 8 - SqlJasonView Answer on Stackoverflow
Solution 9 - SqloᴉɹǝɥɔView Answer on Stackoverflow