Example of three valued logic in SQL Server

后端 未结 5 1002
难免孤独
难免孤独 2020-12-16 05:02

I understand that SQL uses three valued logic but I am having trouble understanding how to use this in practice, especially why TRUE || NULL = True and FA

5条回答
  •  感情败类
    2020-12-16 05:36

    The code example by user4955163 is a great visualization of this, however I just wanted to step back in to address the first segment of the question:

    ...especially why TRUE || NULL = True and FALSE && NULL = False instead of evaluating to null...

    TRUE || NULL = True
    

    This is because the or operator will short-circuit if one operand is already known to be true. No matter what the second operand is (even if unknown, ie. "NULL"), it wouldn't make the expression false since we already know the other operand is true. or only needs one operand to be true, to evaluate to true.

    FALSE && NULL = False
    

    This is because the and operator will short-circuit if one operand is already known to be false. No matter what the second operand is (even if unknown, ie. "NULL"), it wouldn't make the expression true since we already know the other operand is false. and needs both operands to be true to evaluate to true.

提交回复
热议问题