August 24, 2010 at 5:41 pm
Five pages about NULL and not one mention of using NULLIF to test against a datum...
August 24, 2010 at 7:07 pm
Heh... nah... 5 pages about the philosophy of NULL instead. So much for not hijacking this thread.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 25, 2010 at 8:27 am
5 pages of discussion, but no answer to the question asked a few times. Does SQL 2008 handle NULLs differently from SQL 2005 or SQL 2000? The title implies there is a difference but doesn't discuss it.
-- Kit
August 25, 2010 at 8:37 am
Kit G (8/25/2010)
5 pages of discussion, but no answer to the question asked a few times. Does SQL 2008 handle NULLs differently from SQL 2005 or SQL 2000? The title implies there is a difference but doesn't discuss it.
Kit, that's one way of interpreting it. Another is that SQL Server 2008 is the platform that Adolfo is accustomed to using and on which he did his testing. For that reason, he may have preferred not to imply that what he wrote applies to previous versions - even if it actually does.
John
August 25, 2010 at 9:31 am
That's right, John. I tested my scripts only in 2008, so I thought it appropriate to indicate that. I'm sorry if the title gave the impression that there is something new about nulls in 2008.
August 25, 2010 at 12:56 pm
tim.stevens (8/24/2010)
This is one of the best concise treatises on NULL I have seen. Your adminition about a well architected database design not allowing NULLs for any columns is as true as it is bold. The concept of NULL is, quite simply, a flawed one and really has not business being a part of the relational model (See E.F. Codd, The Relational Model For Database Management, ISBN 0-201-14192-2). That aside, having a definitive (and informed) strategy for handling these pesky buggers saves hours of hair-pulling.
You should have mentioned that EF Codd and CJ Date (and others) conducted a very long, very pointed, and very public debate concerning the role of NULL in the relational calculus. As I recall, Date had a column titled "According to Date" in one of the monthly mags (?Database Programming and Design?) from Ziff Davis or Miller Freeman (it has been a long time:-P ).
Certainly there is academic interest in various theoretical ideals such as nth Normal Form, NULL-less schemas, etc. Those of us faced with more practical problems need pragamitic solutions.
We generally accept that something approaching 3rd Normal is adequate and appropriate for most OLTP situations. Similarly, when properly defined and implemented NULL can yield a nice concise data model that is very efficient and safe. As one example, I have found it very useful in relationship tables that must represent mulitple, changing relationships over time. The "Current" department(s) for a broker is the one whose EndDate is NULL. Previous departments are represented with Start and End date values. This particular implementation permitted multiple, overlapping, and concurrent assignments.
August 25, 2010 at 4:38 pm
It would also be helpful to expand a bit on string manipulations using NULL. While this nice article is restricted to the DB Engine, I recently had to dig to find out why SSIS was attempting to insert a null string into a varchar column set to disallow NULL. 😉
Turns out that SSIS string concatenation in the Derived Column Data Task will set the column to NULL if any of the input columns is NULL. The work around is using the SSIS ISNULL(<column>,TRUE, FALSE) for each expression... with TRUE being set to an empty string.... and FALSE being the original column.
August 26, 2010 at 9:16 pm
Adolfo J. Socorro
main cause of confusion, I would say, is thinking that NULL means blank or empty.
Microsoft
A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value.
If that is true then, what is an empty value? AFAIK, NULL is the only way to leave a field empty!
I think it would be more reasonable to say that from the point of view of a logical or arithmetic operation a NULL value is considered unknown since it cannot be resolved to an actual value so it is unknown within the context of the operation and that would make the result of the operation unknown as well, but from the point of view of data storage it is actually a blank or empty field.
Socorro
One way to avoid worrying about NULLs is never to use them, always declaring columns as not allowing NULLs and designating default values for "empty" or "unknown". This will save you keystrokes, especially when you want to check whether a column does not have a certain value.
I don't see how that will save you significant amount of time, checking for a NULL is just as easy as checking for any value and it will make your code more readable. Using a magic number is something you would want to do if you didn't had NULL support, it doesn't have any advantages over NULLs (you'll still have to check for the magic value implicitly to find out if it was set), and it has some disadvantages, of the top of my head:
1) It can screw up greater than/less than queries (the empty fields may come up on the query when they're not supposed to);
2) If you do it on a foreign key then you'll need a dummy record on the referenced table;
3) Most front-end data frameworks can handle NULL values without any special handling, for example you can store a NULL value in any nullable data type or use it to set a GUI control directly, a magic value will always require some special handling;
4) Any qualified developer should understand the concept of NULLs but may not understand the logic behind your magic value.
August 26, 2010 at 9:25 pm
Don Gilman, P.E. (8/25/2010)
It would also be helpful to expand a bit on string manipulations using NULL. While this nice article is restricted to the DB Engine, I recently had to dig to find out why SSIS was attempting to insert a null string into a varchar column set to disallow NULL. 😉Turns out that SSIS string concatenation in the Derived Column Data Task will set the column to NULL if any of the input columns is NULL. The work around is using the SSIS ISNULL(<column>,TRUE, FALSE) for each expression... with TRUE being set to an empty string.... and FALSE being the original column.
The reason for that is the same reason why NULL + 5 = NULL, because NULL is considered unknown in all operations, including string concatenation, if you concatenate an unknown string to a known one the result will logically be unknown or NULL.
He did cover that when he said "Also, any non-logical expressions involving NULLs have an unkown, or NULL, result."
August 26, 2010 at 10:04 pm
AFAIK, NULL is the only way to leave a field empty!
I disagree: setting a field to NULL does not 'leave [or make] a field empty', instead it makes (or should make) any field value, and even the existence of such a value, undefined, inaccessible and irrelevant. That is what I meant before when I wrote before that NULL is a state: just stop thinking of NULL as a value!
August 26, 2010 at 11:36 pm
Alex-668179 (8/26/2010)
AFAIK, NULL is the only way to leave a field empty!
I disagree: setting a field to NULL does not 'leave [or make] a field empty', instead it makes (or should make) any field value, and even the existence of such a value, undefined, inaccessible and irrelevant. That is what I meant before when I wrote before that NULL is a state: just stop thinking of NULL as a value!
Z'actly. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
August 27, 2010 at 12:41 am
frodriguez.im (8/26/2010)
what is an empty value? AFAIK, NULL is the only way to leave a field empty!
For string columns and variables, the empty string is an often-used synonym for the zero-length string: ''.
I guess a varbinary could also be considered empty when the contents are zero-length. Other data types do not support an empty value, as there are no empty values in the various numeric domains, nor in the date, time, or datetime domains. (Maybe xml does support some kind of empty value, though I think you can only do that with untyped xml - but I am far from an expert in the field of xml, so I might be wrong).
September 1, 2010 at 4:56 pm
This next table summarizes the effect of NULLs in AND expressions:
AND True False NULL
True True False NULL
False False False False
NULL NULL False NULL
This second table summarizes the effect of NULLs in OR expressions:
OR True False NULL
True True True True
False True False NULL
NULL True NULL NULL
I tested both of these truth tables for the underlined conditions:
false AND null
and
true OR null
because I wasn't sure about the behavior here. The programmer in me said short circuiting might work, while the ternary logician in me misremembered a NULL result in both of those conditions. On both my 2k8 and 2k5 installations, I only got the OR behavior to match the article. Is there a setting I'm missing? I tried this with ansi_nulls both off and on with the same result.
using this SQL:
if(1=1 or 1=null)
print 'test passed'
else
print 'test didn''t pass'
if(1=0 and 1=null)
print 'test passed'
else
print 'test didn''t pass'
My output is:
test passed
test didn't pass
September 2, 2010 at 12:02 am
oscar.leeper (9/1/2010)
On both my 2k8 and 2k5 installations, I only got the OR behavior to match the article. Is there a setting I'm missing?
No, you are making a logic error. Your test does not distinguish between Unknown (sorry for being pedantic, but the third value in three-valued logic is NOT NULL, but Unknown). According tho the truth tables, the AND test should return False. And it does, but you'd get the same output if it did return Unknown.
Try it with this code:
if(1=1 or 1=null)
print 'test passed'
else
if not(1=1 or 1=null)
print 'test didn''t pass'
else
print 'test result unknown'
if(1=0 and 1=null)
print 'test passed'
if not(1=0 and 1=null)
print 'test didn''t pass'
else
print 'test result unknown'
September 2, 2010 at 3:37 am
I try to avoid NULLs, as far as is practicable, in my designs. They complicate coding and often result in suboptimal query performance.
In suitable cases, I have no problem with so-called 'magic values', if they make logical sense. For example, I accept the idea of using a value like '9999-12-31' for an end date rather than using NULL.
One thing I did not like in the article was the repeated use of constructions like WHERE ISNULL(column, magic_value) <> test_value. Whether you try to handle NULL's inconvenient behaviour with a CASE statement (or the equivalent COALESCE or NULLIF expressions) or ISNULL, the result is the same: a non-SARGable expression.
Using an explicit OR seems preferable to me - and you at least give the optimizer a fighting chance at finding an efficient plan. In many cases, rewriting the query as a UNION of the non-NULL and NULL conditions works better still.
Anyone who has ever had to write a (correct) query to determine if a NULLable column has changed surely shares my dislike for the things. Sometimes they are unavoidable, but that doesn't mean it's not worth the attempt.
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 15 posts - 46 through 60 (of 68 total)
You must be logged in to reply to this topic. Login to reply