Not equal <> != operator on NULL

后端 未结 11 1527
南旧
南旧 2020-11-22 02:50

Could someone please explain the following behavior in SQL?

SELECT * FROM MyTable WHERE MyColumn != NULL (0 Results)
SELECT * FROM MyTable WHERE MyColumn <         


        
相关标签:
11条回答
  • 2020-11-22 03:24

    NULL Cannot be compared to any value using the comparison operators. NULL = NULL is false. Null is not a value. The IS operator is specially designed to handle NULL comparisons.

    0 讨论(0)
  • 2020-11-22 03:27

    In SQL, anything you evaluate / compute with NULL results into UNKNOWN

    This is why SELECT * FROM MyTable WHERE MyColumn != NULL or SELECT * FROM MyTable WHERE MyColumn <> NULL gives you 0 results.

    To provide a check for NULL values, isNull function is provided.

    Moreover, you can use the IS operator as you used in the third query.

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 03:30

    Old question, but the following might offer some more detail.

    null represents no value or an unknown value. It doesn’t specify why there is no value, which can lead to some ambiguity.

    Suppose you run a query like this:

    SELECT *
    FROM orders
    WHERE delivered=ordered;
    

    that is, you are looking for rows where the ordered and delivered dates are the same.

    What is to be expected when one or both columns are null?

    Because at least one of the dates is unknown, you cannot expect to say that the 2 dates are the same. This is also the case when both dates are unknown: how can they be the same if we don’t even know what they are?

    For this reason, any expression treating null as a value must fail. In this case, it will not match. This is also the case if you try the following:

    SELECT *
    FROM orders
    WHERE delivered<>ordered;
    

    Again, how can we say that two values are not the same if we don’t know what they are.

    SQL has a specific test for missing values:

    IS NULL
    

    Specifically it is not comparing values, but rather it seeks out missing values.

    Finally, as regards the != operator, as far as I am aware, it is not actually in any of the standards, but it is very widely supported. It was added to make programmers from some languages feel more at home. Frankly, if a programmer has difficulty remembering what language they’re using, they’re off to a bad start.

    0 讨论(0)
  • 2020-11-22 03:32

    NULL has no value, and so cannot be compared using the scalar value operators.

    In other words, no value can ever be equal to (or not equal to) NULL because NULL has no value.

    Hence, SQL has special IS NULL and IS NOT NULL predicates for dealing with NULL.

    0 讨论(0)
  • 2020-11-22 03:32

    We use

    SELECT * FROM MyTable WHERE ISNULL(MyColumn, ' ') = ' ';
    

    to return all rows where MyColumn is NULL or all rows where MyColumn is an empty string. To many an "end user", the NULL vs. empty string issue is a distinction without a need and point of confusion.

    0 讨论(0)
  • 2020-11-22 03:35

    The only test for NULL is IS NULL or IS NOT NULL. Testing for equality is nonsensical because by definition one doesn't know what the value is.

    Here is a wikipedia article to read:

    https://en.wikipedia.org/wiki/Null_(SQL)

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