I think that's the thing. People use NULL to mean empty value, instead of unknown value.
Not necessarily for strings because there's the empty string, but for numeric fields, including foreign keys
SELECT 'foo' WHERE bar = baz FROM quux
UNION ALL
SELECT 'bar' WHERE bar <> baz FROM quux
I would understand returning an error in that case if bar or baz were unknown. The truthiness of bar = baz is unknown, so that's an error.But that's not what SQL Servers do. They're all pedantic about how NULL means unknown right up until the rubber hits the road of the WHERE clause, when suddenly NULL means false.
Which means that they're not saying that foo = bar is unknown, they're instead making a definitive assertion that both foo = bar is false and foo <> bar is false. And that's disastrously wrong.
Were it any other way, joins wouldn’t work properly.
Especially for (composite) unique indexes on NULLable foreign keys, where NULL signifies the non-existence of a different table entry (if you don't want to resort to a special "not existing" entry in that table), this behavior can be surprising and definitely got me a few times.
If two different products have the price of $0 and therefore free, their prices are equal.
If two different products both have prices which are "not known", their unknown prices are not equal.
If two different products both have prices which are "known to not exist" their undefined prices are also not equal.
If SQL servers had algebraic types and I could roll my own "Maybe", then this bizarre decision would work. But they don't, so it doesn't.
The problem with having a value called “none” is that it would be tempting to make none = none return true, which is madness.
(I do agree it would be nice if we could have multiple kinds of null instead of just one. But I would want them all to have the same equality properties as NULL.)
create table test(a bool);
insert into test(a) values (NULL);
select case a when a then true else false end from test;
The answer should be NULL, right?Since null = null is false, your case statement will return the else value. You hard coded it to be false, therefore it will return false.
If you want the lack of data to be acknowledged in a case statement, it’s up to you to handle it. SQL isn’t a magical fairy that does what you want without asking.
That interpretation makes sense to me when I read the docs, but is way too easy to miss when I'm actually doing comparisons.
For example: a shopping cart might contain a tee-shirt in blue colour and expedited shipping in NULL colour. That blue tee-shirt might be the same colour as some blue socks... but that NULL-coloured shipping isn't the same colour as a NULL-coloured charitable donation.
Counter example: SAP HANA doesn't support NULLs, and as a result if I see 0 in any column in the database I have to wonder was it 0 that user submitted or 0 that system added as a placeholder. I would need to go back to documentation and see if the field is mandatory etc. Better have it explicitly stored in the data itself.
Two people with the same middle name would have equality with respect to the string value of middleName. The same applies with two people known to have no middle name—where you store a zero length string.
Two people who didn’t supply a middle name should not have equality with respect to middleName. Hence null. This acknowledged lack of data cannot be equal to a different lack of data.