Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL equality/inequality comparison with nullable values

first take, kludge solution, sentinel approach(it's imperative that your program should not allow inputting of 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 on your program, the user inputted -2147483648 on the B field, and A is null. the code above reports true, should report false, should not report true nor null.

what's the most concise way to compare equality on nullable fields? A == B should just report either true or false only, regardless if the field(s) are nullable or not.

like image 874
Hao Avatar asked Dec 31 '22 03:12

Hao


1 Answers

Probably IS [NOT] DISTINCT FROM will help here.

like image 94
Milen A. Radev Avatar answered Jan 01 '23 18:01

Milen A. Radev