Why does comparing a SQL date variable to null behave in this way?

前端 未结 2 1956
情歌与酒
情歌与酒 2020-12-07 02:10

I just came across an interesting problem with a procedure I am writing in SQL.

In my proc I have 2 dates, which are optional params defaulted to NULL, I want to che

相关标签:
2条回答
  • 2020-12-07 02:42

    Care must be taken when comparing null values. The behavior of the comparison depends on the setting of the SET ANSI_NULLS option.

    When SET ANSI_NULLS is ON, a comparison in which one or more of the expressions is NULL does not yield either TRUE or FALSE; it yields UNKNOWN. This is because a value that is unknown cannot be compared logically against any other value. This occurs if either an expression is compared to the literal NULL, or if two expressions are compared and one of them evaluates to NULL.

    See NULL Comparison Search Conditions

    0 讨论(0)
  • 2020-12-07 03:05

    Simply put 'NULL' does not equal 'NULL'. 'NULL' is comparable to a state of uncertainty, where one thing being uncertain does not necessarily equal something else that is also uncertain. Use 'IS NULL', 'ISNULL()', or 'COALESCE()' when testing for nulls. Setting ANSI_NULLS to 'off' can change this behavior, but it is not the ANSI SQL standard. See http://msdn.microsoft.com/en-us/library/ms191270.aspx for more info.

    0 讨论(0)
提交回复
热议问题