• sknox (12/30/2010)


    Any boolean expression is capable of being short-circuited, in the right circumstances.

    So under what circumstances can you short-circuit an XOR? (i.e, if either A or B but not both then C)?

    T-SQL lacks a XOR logical operator, but it can be implemented from its definition:

    A XOR B = (A AND NOT B) OR (NOT A AND B)

    Sorry for the stupid example, I can't think of a better one right now: to find all users with NULL first_name (expression A) or NULL middle_name (expression B) but not both you could write:

    -- This is how you would do it if T-SQL had a XOR operator.

    SELECT *

    FROM user

    WHERE (first_name IS NULL) XOR (middle_name IS NULL)

    -- This is how you have to code it with AND, OR and NOT operators

    SELECT *

    FROM user

    WHERE (first_name IS NULL AND middle_name IS NOT NULL)

    OR (first_name IS NOT NULL AND middle_name IS NULL)

    Any boolean operator can be rewritten using AND, OR and NOT.

    -- Gianluca Sartori