Question

I get different results when i run select queries with != NULL OR IS NOT NULL .

    /** No results returned.*/
        SELECT *
        FROM PORT_INFO
        WHERE PORT_CODE != NULL;

   /** Results are returned.*/
    SELECT *
    FROM PORT_INFO
    WHERE PORT_CODE IS NOT NULL;
  • In My Table, PORT_CODE column is nullable.
  • Column is of type VARCHAR(4) .
  • MySql Version 5.5.13 enterprise.
Was it helpful?

Solution

From the documentation:

Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons.

So basically IS NOT NULL will actually do what you intend, while != will do nothing useful (since it too will just return NULL).

OTHER TIPS

In SQL, there is no value that is equal to NULL, including NULL itself. To get what you expect, use IS NOT NULL.

Any comparison or operation with NULL returns NULL, with the exception of IS NULL.

A NULL boolean is considered to be false.

So, expressions like <> NULL, = NULL, and so on are always false.

This is ANSI standard behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top