12

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.

5
  • In sql Null is defined as Null and '' refers to some empty value Commented Nov 17, 2016 at 9:58
  • 1
    '' Empty string: We can say there is no value. But NULL means we cant say there is no value. It means there might be have value, but its unknown. That is the reason why NULL is different from empty string. Commented Nov 17, 2016 at 9:59
  • Don't allow zero length strings as name (have a constraint, or trigger). Use NULL in that case. Commented Nov 17, 2016 at 9:59
  • NULL is NULL, blank is blank.... Commented Nov 17, 2016 at 9:59
  • Null means it contains 'NULL' as a value and '' means not any value Commented Nov 17, 2016 at 10:01

5 Answers 5

17

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.
Sign up to request clarification or add additional context in comments.

2 Comments

by selecting cell IS NULL and by cell=''
Actually, null = null will result in UNKNOWN, see learn.microsoft.com/en-us/sql/t-sql/language-elements/….
2

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.

Comments

2
  • 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

Comments

1

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

Comments

0

It is because a cell containing NULL and an empty cell are 2 different things. i.e. -> NULL is not as same as '' or "".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.