SQL equality / inequality comparison with nullable values

first accept, kludge decision, sentinel approach (it is necessary that your program does not allow you to enter a sentinel value):

 select coalesce(a, -2147483648) = coalesce(b, -2147483648) as is_equal -- a little postgresism

let's say you forgot to block the sentinel value in your program, the user entered -2147483648 in field B, and A - null. the code above reports true, should report false, should not report true and null.

What is the most concise way to compare field equality with a zero value? A == B should just report only true or false, whether the fields are null or not.

+4
source share
2 answers

<= > , ; postgres? a is null and b is null or (a is not null and b is not null and a == b) . , FALSE AND NULL FALSE.

0

All Articles