Why does NULL = NULL evaluate to false in SQL server

前端 未结 19 1534
一向
一向 2020-11-22 05:02

In SQL server if you have nullParam=NULL in a where clause, it always evaluates to false. This is counterintuitive and has caused me many errors. I do understa

19条回答
  •  囚心锁ツ
    2020-11-22 05:31

    Because NULL means 'unknown value' and two unknown values cannot be equal.

    So, if to our logic NULL N°1 is equal to NULL N°2, then we have to tell that somehow:

    SELECT 1
    WHERE ISNULL(nullParam1, -1) = ISNULL(nullParam2, -1)
    

    where known value -1 N°1 is equal to -1 N°2

提交回复
热议问题