This is my simplified statement
SELECT ...
FROM tab1 AS i FULL OUTER JOIN tab2 AS d ON i.[Id]=d.[Id]
WHERE d.[Data]<>i.[Data] OR
(d.[Data] IS NULL AND i.[Data] IS NOT NULL) OR
(d.[Data] IS NOT NULL AND i.[Data] IS NULL)
I want to get all entries that are
- i.[Data] is different from d.[Data]
- At least one value in table i or d is NOT NULL
So I don't want to see records were and i and d contain the same data or are both NULL.
My statement look so long and complicated. Is there an easier way?
Using ISNULL(d.[Data],'')<>ISNULL(i.[Data],'')
works for text, but not for DATE
or TIME(0)
columns.
My statement works for every type.
3条答案
按热度按时间5rgfhyps1#
Yes you can, and you can get the optimizer to recognize it too.
Paul White has this little ditty :
This works because of the semantics of
INTERSECT
which deal with nulls. What this says is "are there no rows in the subquery made up of value B and value B", this will only be satisfied if they are different values or one is null and the other not. If both are nulls, there will be a row with a null.If you check the XML query plan (not the graphical one in SSMS), you will see that it compiles all the way down to
d.[Data] <> i.[Data]
, but the operator it uses will haveCompareOp="IS"
and notEQ
.See the full plan here .
The relevant part of the plan is:
I find the optimizer works very well this way round, rather than doing
EXISTS / EXCEPT
.I urge you to vote for the Azure Feedback to implement a proper operator.
UPDATE FOR SQL SERVER 2022: This operator is now available as
IS [NOT] DISTINCT FROM
.iqjalb3h2#
You can simplify to:
See live demo.
qacovj5a3#
A new option is now available in SQL Server 2022, which has now implemented the ISO-standard
IS [NOT] DISTINCT FROM
syntax mentioned in the comments.This compiles into an
IS
comparison underneath (the same as in my other answer usingEXCEPT
), and is very efficient.