views:

531

answers:

2

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.

A: 

The operator <=> does what you want in other database engines; is it not in postgres? Otherwise, a is null and b is null or (a is not null and b is not null and a == b) should work. This works because FALSE AND NULL is FALSE.

Doug
doesn't work on postgresql(or other rdbms for that matter). lucky mysql bastards for that operator :-p anyway, +1 just in case some mysqlers chance upon this problem
Hao
At least with Oracle, the usual operators do NOT work as expected with null values...
Thilo
+7  A: 

Probably IS [NOT] DISTINCT FROM will help here.

Milen A. Radev