Tag Archives: Comparison

Equating NULLs with EXISTS + INTERSECT

Can you rewrite WHERE Col1 = Col2 OR (Col1 IS NULL AND Col2 IS NULL) so that each column name is only used once without using the substitution method (that is, without doing something like WHERE ISNULL(Col1, '-2,147,483,648') = ISNULL(Col2, '-2,147,483,648'))?

WHERE EXISTS(SELECT Col1 INTERSECT SELECT Col2)

What…what?! That can’t be possible! I thought INTERSECT compares two result sets, not two columns from the same row of data. Continue reading