Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««34567

The Semantics of NULL in SQL Server 2008 Expand / Collapse
Author
Message
Posted Thursday, September 2, 2010 10:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:46 PM
Points: 33, Visits: 85

if(1=0 and 1=null)


Does ternary logic have the same rule that allows me to to say that the above is logically equivalent to

if 1!=0 or 1!=null

If so, I'm back where I started, since in my original example,

if 1=0 and 1=null

did not pass, but its logical equivalent,

if 1!=0 or 1!=null

does.
Post #979721
Posted Thursday, September 2, 2010 3:25 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:07 PM
Points: 6,002, Visits: 8,266
oscar.leeper (9/2/2010)

if(1=0 and 1=null)


Does ternary logic have the same rule that allows me to to say that the above is logically equivalent to

if 1!=0 or 1!=null

Well, yes and no.
It is true that in ternary (three-valued) logic, just as in boolean (two-valued) logic, A and B is completely equivalent to not(not(A) or not(B)). But that not only means that if A and B, is true, not(A) or not(B) is false and if A and B, is false, not(A) or not(B) is true - it also means that if if A and B, is unknown, not(A) or not(B) is unknown as well, since not(unknown) is unknown.

If so, I'm back where I started, since in my original example,

if 1=0 and 1=null

did not pass, but its logical equivalent,

if 1!=0 or 1!=null

does.

The problem is that you use a simple IF to test things about ternary logic. But a simple IF has only two possible outcomes - one for true, one for both false and unknown. You would have gotten the expected results if you had used

if 1=0 and 1=null

and

if not(1<>0 or 1<>null)

But you omitted the not in the last one and switched the then and else clauses of the if. Which is fine for the truth values true and false, but not for the truth value unknown. If A is unknown, then NOT(A) is unknown as well, so that both "IF (A)" and "IF NOT(A)" will fall through to the else clause.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #979935
Posted Friday, September 3, 2010 9:18 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, September 4, 2010 4:52 PM
Points: 8, Visits: 16
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!


I actually agree with most of that except for the "any value" part, a field cannot be any value or any logical operation on it would always return TRUE, replace any value with "an unknown value" and your definition is 100% correct.

The use of the term blank or empty field comes from thinking of the stage when the data is inputed, normally somebody will enter the data on a form and any fields that are unknown at the time (inaccessible) or not applicable (irrelevant) will be left blank or empty, when the data makes it to the database one way to express that the field was left blank is to set it to NULL, there may be many cases when normalization or some other method is a better choice, but in many cases using NULL is the best choice.
Post #980575
Posted Friday, September 3, 2010 9:41 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, September 4, 2010 4:52 PM
Points: 8, Visits: 16
Hugo Kornelis (8/27/2010)
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).


I thought about that when I was writing my comment, my rationale for not mentioning it is that:

a) NULL is type independent and these are not so they're sort of not in the same context (in my head at least :)); and
b) although they are widely accepted, in essence these are magic values so it's not really empty.

ps. you can use an empty string to set an "empty" xml field, you can also use an empty document header or any xml that doesn't really contains any data, either way when you query it and convert it to a string you get an empty string.
Post #980577
Posted Friday, September 3, 2010 11:22 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 7:17 PM
Points: 197, Visits: 459
I actually agree with most of that except for the "any value" part, a field cannot be any value or any logical operation on it would always return TRUE...


I don't understand your comment or perhaps you misread my comment or I misread yours. First of all I never said or suggested that a NULL field has "any value"; instead I said that "any value" should be ignored etc. because the field should be treated as not having a value. Second I disagree with the statement that even if I had implied that a NULL field had "any value", then every logical operation would return TRUE. Perhaps you are confusing this with a convention in mathematics which says exactly the opposite: that any statement about the elements of an empty set is TRUE.

The reason why I used so many words to say that the field should be treated as not having a value, is that apparently this simple statement is not enough. People still continue to discuss, for example, what NULL means for the value of the field (is it missing, or unknown, or what...) whereas such a discussion is totally inappropriate. Considering the mathematical background of the relational model the only interpretation of NULL that makes any sense to me is the minimalistic interpretation that there is no value. Period. Anything beyond that is too much speculation when it is done in discussing the meaning of NULL in general.
Post #980595
Posted Saturday, September 4, 2010 4:52 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, September 4, 2010 4:52 PM
Points: 8, Visits: 16
Alex-668179 (9/3/2010)
I actually agree with most of that except for the "any value" part, a field cannot be any value or any logical operation on it would always return TRUE...


I don't understand your comment or perhaps you misread my comment or I misread yours. First of all I never said or suggested that a NULL field has "any value"; instead I said that "any value" should be ignored etc. because the field should be treated as not having a value. Second I disagree with the statement that even if I had implied that a NULL field had "any value", then every logical operation would return TRUE. Perhaps you are confusing this with a convention in mathematics which says exactly the opposite: that any statement about the elements of an empty set is TRUE.

The reason why I used so many words to say that the field should be treated as not having a value, is that apparently this simple statement is not enough. People still continue to discuss, for example, what NULL means for the value of the field (is it missing, or unknown, or what...) whereas such a discussion is totally inappropriate. Considering the mathematical background of the relational model the only interpretation of NULL that makes any sense to me is the minimalistic interpretation that there is no value. Period. Anything beyond that is too much speculation when it is done in discussing the meaning of NULL in general.


I misread yours, I had too much to drink last night. If you had implied that the field could be any value then it could be the value that will fulfill the condition tested for and it would return TRUE but since it's not realistic that the field could have any value it's not worth discussing, my mistake.

Now how do you differ an empty or blank field from one with no value? I understand them to be the same.
Post #980700
Posted Saturday, September 4, 2010 5:14 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:07 PM
Points: 6,002, Visits: 8,266
frodriguez.im (9/4/2010)
Now how do you differ an empty or blank field from one with no value? I understand them to be the same.

In a varchar column, the empty value is '' - that is an opening and an ending quote with nothing in between. A great way to represent the middle initial of George Washington, as he had no middle initial.
In the same column, no value would be represented by NULL, the special marker to indicate the absence of a value. A great way to represent the middle intial of the 45th president of the United States, as noone currently knows what (if any!) middle initial he or she has.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #980704
Posted Saturday, September 4, 2010 9:32 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 7:17 PM
Points: 197, Visits: 459
In my opinion a more informative comparison would be between NULL and empty string in the same field, e.g.middle name of George Washington. Empty string means "no middle name" (or a data error if there actually is one). NULL means that nothing is said about whether there is a middle name or not and what it's value might be if there is one.

That's the general idea but of course nothing stops people from giving their own interpretation to what NULL means in a concrete situation. But that would be a modification of the original NULL concept.
Post #980732
Posted Monday, September 6, 2010 12:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:34 PM
Points: 37,098, Visits: 31,648
Hugo Kornelis (9/4/2010)
frodriguez.im (9/4/2010)
Now how do you differ an empty or blank field from one with no value? I understand them to be the same.

In a varchar column, the empty value is '' - that is an opening and an ending quote with nothing in between. A great way to represent the middle initial of George Washington, as he had no middle initial.
In the same column, no value would be represented by NULL, the special marker to indicate the absence of a value. A great way to represent the middle intial of the 45th president of the United States, as noone currently knows what (if any!) middle initial he or she has.


Exactly! Hugo for President!


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #981224
« Prev Topic | Next Topic »

Add to briefcase «««34567

Permissions Expand / Collapse