Diamond Notes

Just another WordPress weblog

Tip of the Day — NULL != Empty String

Thanks to David, our Jr. DBA, we get the tip of the day. Did you realize that NULL (the topic of another post here) is not equal to the empty string (’ ‘). It is true. If you compare a NULL and an empty string you can see this:

mysql> select ‘ ‘ is NULL;
+————-+
| ‘ ‘ is null |
+————-+
| 0 |
+————-+
1 row in set (0.05 sec)

mysql> select ‘ ‘ is not NULL;
+—————–+
| ‘ ‘ is not NULL |
+—————–+
| 1 |
+—————–+
1 row in set (0.00 sec)

The zero returned means false, the one means true. This has implications to your code. For example, since an empty string is not NULL it can be inserted into a column that is set to NOT NULL.

For a more in depth discussion take a look here: http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html

3 Comments so far

  1. Hubert Roksor March 21st, 2008 9:36 am

    That reminds me of the oldest way to have a faux-BIT column, as a NULL-able CHAR(0) column where the only two possible values are NULL and ” :) (which is definitely not a good practice, though)

  2. Maria Ozawa March 21st, 2008 11:19 am

    What if no zero returned?

  3. admin March 21st, 2008 11:45 am

    I am not sure what you mean Maria, it does return either a zero or a one.

Leave a reply