Empty string vs NULL

2019-06-15 18:43发布

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.

4条回答
三岁会撩人
2楼-- · 2019-06-15 19:33

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.
查看更多
Evening l夕情丶
3楼-- · 2019-06-15 19:34

Some differences between them:

  • NULL can be assigned to any type, as opposed to empty string which won't be compatible with date/numerical fields.
  • NULL is an UNKNOWN value, it doesn't have a value as opposed to an empty string, which is a value, but empty one.
  • As far as I'm aware of, NULL shouldn't capture memory as opposed to an empty string which does.
  • null = null will result in null as opposed to ''='' which will result in TRUE.
查看更多
Explosion°爆炸
4楼-- · 2019-06-15 19:47
  • Null is an absence of a value. An empty string is a value, but is just empty.

  • Null is special to a database.

  • Null has no bounds, it can be used for string, integer, date, etc. fields in a database.
  • NULL isn't allocated any memory, the string with NUll value is just a pointer which is pointing to nowhere in memory. however, Empty IS allocated to a memory location, although the value stored in the memory is "".

By using NULL you can distinguish between "put no data" and "put empty data".

Some more differences:

A LENGTH of NULL is NULL, a LENGTH of an empty string is 0. NULLs are sorted before the empty strings. COUNT(message) will count empty strings but not NULLs You can search for an empty string using a bound variable but not for a NULL. This query:

SELECT  *
FROM    mytable 
WHERE   mytext = ?

will never match a NULL in mytext, whatever value you pass from the client. To match NULLs, you'll have to use other query

SELECT * FROM mytable WHERE mytext IS NULL

查看更多
beautiful°
5楼-- · 2019-06-15 19:48

He can't find NULL because it doesn't have a value

An empty string is a value, but its empty.

Only if its a value it can compare with another value

查看更多
登录 后发表回答