NULL
is the absence of a value and usually indicates something meaningful, such as unknown or not defined (yet). For example, if I start a project today, StartDate
is 2012-02-25
. If I don't know how long the project will take, what should EndDate
be like? Maybe I know what ProjectedEndDate
might be, but I would set EndDate
to NULL
and update it when the project is complete.
''
- a string with zero length (or "empty"). Technically, this is a lack of data, as this may make sense. For example, if I don’t have a middle name, depending on your data model, ''
may make more sense than NULL
, since the latter implies obscurity, but ''
may mean that it is known that I do not have one. NULL
can be used in the same way, but then it is difficult to decipher, it is not known whether it exists or it is known that it does not exist. Many standards have special meanings for things where this may not be known - for example, Gender
I count 9 different character codes, so if M
or F
not specified, we always know exactly why (unknown, not specified, transgender, etc. .). Also think about where HeartRate
NULL
- is it because there was no impulse or because we have not yet received it?
This is not the same thing, but, unfortunately, many people treat them the same way. If your column allows NULL
, it means that you know in advance that sometimes you may not know this information. If you do not view them as one and the same, your requests will be different. For example, if col
does not allow NULL
, your first query will always return all the results in the table, since none of them can be NULL
. However, NOT NULL
still allows you to enter an empty string, unless you also set a control limit to prevent zero-length strings.
The resolution for the same column is usually a bit confusing for someone trying to understand the data model, although I believe that in most cases the NOT NULL
does not go with the LEN(col)>0
constraint. The problem, if both are allowed, is that it is difficult to understand what this means if the NULL
column or the column is “empty” - they may mean the same thing, but they may not be - and this will vary from store to store .
Another key point is that NULL
is evaluated as unknown compared to anything (at least by default in SQL Server *
), which in turn is evaluated as false. As an example, these queries return 0
:
DECLARE @x TABLE(i INT); INSERT @x VALUES(NULL); SELECT COUNT(*) FROM @x WHERE i = 1; SELECT COUNT(*) FROM @x WHERE i <> 1; SELECT COUNT(*) FROM @x WHERE i <= 3; SELECT COUNT(*) FROM @x WHERE i > 3; SELECT COUNT(*) FROM @x WHERE i IN (1,2,3); SELECT COUNT(*) FROM @x WHERE i NOT IN (1,2,3);
Since comparisons in the where clause are always evaluated as unknown, they always return to false, so no row ever matches the criteria, and all values are returned as 0
.
Also, the answers to this question on dba.stackexchange may be helpful:
https://dba.stackexchange.com/questions/5222/why-shouldnt-we-allow-nulls
*
You can change this using SET ANSI_NULLS OFF
- however, this is not recommended, because it provides non-standard behavior, or because this function was deprecated with SQL Server 2005 and will become no-op in a future version of SQL Server . But you can play with the query above and see that NOT IN
behaves differently with SET ANSI_NULLS OFF
.