When playing with MySQL I noticed that if I run
SELECT * FROM table WHERE !value;
returns the same thing as
SELECT * FROM table WHERE value IS NOT NULL;
The table is "employees" and the column is "MiddleInitial" (strings). So I get all employees who's MiddleInitial is not null.
Is this a proper shorthand or just a coincidence? I am wondering if this is a safe way to write? I cannot seem to find any information on this.
I was expecting
SELECT * FROM table WHERE !value;
to return all null values. Oddly enough
SELECT * FROM table WHERE value;
returns nothing.
No, it's not.
It might seem like it because it's doing type coercion to force whatever is in the column into a boolean value.
NULLvalues will coerce tofalsewhen forced as a boolean predicate, and most non-null column values will coerce totrue. But some column values (that are not null) will also coerce tofalse.You can see examples here:
Notice the last example is missing the
0row. Also notice the one before that does not include thenullrow, which leads me to suspect your server might have an option set for non-standard null handling.Here's a few more samples:
Notice the treatment of the
'1'row.