T-SQL XOR Operator

后端 未结 9 568
既然无缘
既然无缘 2020-11-30 23:43

Is there an XOR operator or equivalent function in SQL Server (T-SQL)?

相关标签:
9条回答
  • 2020-12-01 00:33

    How about this?

    (A=1 OR B=1 OR C=1) 
    AND NOT (A=1 AND B=1 AND C=1)
    

    And if A, B and C can have null values you would need the following:

    (A=1 OR B=1 OR C=1) 
    AND NOT ( (A=1 AND A is not null) AND (B=1 AND B is not null) AND (C=1 AND C is not null) )
    

    This is scalable to larger number of fields and hence more applicable.

    0 讨论(0)
  • 2020-12-01 00:41

    From your comment:

    Example: WHERE (Note is null) ^ (ID is null)

    you could probably try:

    where
       (case when Note is null then 1 else 0 end)
     <>(case when ID is null then 1 else 0 end)
    
    0 讨论(0)
  • 2020-12-01 00:43

    As clarified in your comment, Spacemoses, you stated an example: WHERE (Note is null) ^ (ID is null). I do not see why you chose to accept any answer given here as answering that. If i needed an xor for that, i think i'd have to use the AND/OR equivalent logic:

    WHERE (Note is null and ID is not null) OR (Note is not null and ID is null)
    

    That is equivalent to:

    WHERE (Note is null) XOR (ID is null)
    

    when 'XOR' is not available.

    0 讨论(0)
提交回复
热议问题