Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL vs empty string

Tags:

sql-server

What is the difference between the below queries & how it works?

SELECT * FROM some_table WHERE col IS NOT NULL 

&

SELECT * FROM some_table WHERE col <> ''

Regards, Mubarak

like image 422
user873779 Avatar asked Feb 25 '12 14:02

user873779


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.

Does empty string take more space than null?

Learn MySQL from scratch for Data Science and Analytics In innoDB, NULL occupies less space as compared to empty string. Also, the NULL length is null while length of the empty string is 0. From the above output it is clear that the length of the empty string is 1. The above output means that count is 0 for null value.

Does empty string evaluate to 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 same as empty string in MySQL?

In PHP, the empty string equals to a NULL value, but in MySQL, the case is the different i.e. empty string is not equal to NULL value.


1 Answers

The NULL is special data type, it means absence of value.

An empty string on the other hand means a string or value which is empty.

Both are different.

For example, if you have name field in table and by default you have set it to NULL. When no value is specified for it, it will be NULL but if you specify a real name or an empty string, it won't be NULL then, it will contain an empty string instead.

like image 56
Sarfraz Avatar answered Oct 22 '22 11:10

Sarfraz