Why does NULL = NULL evaluate to false in SQL server

前端 未结 19 1561
一向
一向 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:34

    Just because you don't know what two things are, does not mean they're equal. If when you think of NULL you think of “NULL” (string) then you probably want a different test of equality like Postgresql's IS DISTINCT FROM AND IS NOT DISTINCT FROM

    From the PostgreSQL docs on "Comparison Functions and Operators"

    expression IS DISTINCT FROM expression

    expression IS NOT DISTINCT FROM expression

    For non-null inputs, IS DISTINCT FROM is the same as the <> operator. However, if both inputs are null it returns false, and if only one input is null it returns true. Similarly, IS NOT DISTINCT FROM is identical to = for non-null inputs, but it returns true when both inputs are null, and false when only one input is null. Thus, these constructs effectively act as though null were a normal data value, rather than "unknown".

提交回复
热议问题