Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty string vs NULL

I've a table where some rows have some blank cells. I tried to select such rows using IS NULL function. But the query select 0 rows.

select * from zzz_fkp_registration_female where fname is null;
(0 row(s) affected)

Now I changed my query to:

select * from zzz_fkp_registration_female where fname is null or fname ='';

I got the desired result.

Why is IS NULL not giving the result? What is the difference between IS NULL and '' since the cells are empty. Please explain.

like image 437
nischalinn Avatar asked Nov 17 '16 09:11

nischalinn


People also ask

Should I use empty string or null?

NULL or Empty String: Which One to Use An empty string is a string instance of zero length. However, a NULL has no value at all. We can use a test database and apply the knowledge in the production environment to understand the concept better.

Is Empty the same as null?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.

Why is empty string NOT null?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.

Is null and empty string the same in SQL?

NULL is used in SQL to indicate that a value doesn't exist in the database. It's not to be confused with an empty string or a zero value. While NULL indicates the absence of a value, the empty string and zero both represent actual values.


1 Answers

You can have your query modified as below:

select * from zzz_fkp_registration_female where isnull(fname,'') = '';

This query will result all the blanks cells as well as cell with null values.

Note:

  1. NULL values represent missing unknown data.
  2. Blank data is actual data entered as blank during input.
like image 128
Paresh J Avatar answered Nov 06 '22 10:11

Paresh J