Table 1:
ID email
A [email protected]
Table 2:
ID email2 email3 email4
A [email protected] null [email protected]
Query:
select T1.ID, T1.email,
case when T1.email<>T2.email2 then T2.email2 end email2,
case when T1.email<>T2.email3 and T2.email2<>T2.email3 then T2.email3 end email3,
case when T1.email<>T2.email4 and T2.email2<>T2.email4 and T2.email3<>T2.email4 then T2.email4 end email4
from t1
left join t2 on t1.id=t2.id
Output:
ID email email2 email3 email4
A [email protected] null null null
Expected Output:
ID email email2 email3 email4
A [email protected] null null [email protected]
It looks like email4
evaluates to null because email3
is null. I think I understand why as per Why does NULL = NULL evaluate to false in SQL server, but is there an easy way for me to avoid that from happening? The real query is alot more complex and if I add something like isnull(value,'')
around every variable in the case statement to avoid comparing to a null value, that can get abit much.
Note that I dont have permission to change ansi_nulls
settings and such