• 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/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/