SQL Server

How to Compare Two Values When One is NULL in SQL Server

Say you're comparing two SQL Server columns and you want to know how many rows are different. No problem, you think:

select count(1)
from sessions
where start_date != end_date;

Not so fast. If some of the start or end dates are null, they won't be counted! Surely that's not what you wanted. That's why you need to use the INTERSECT operator:

select count(1)
from sessions
where not exists(select start_date intersect select end_date);

Now, your count will be “null aware” and you'll get the result you want.