0

This is my xml query

declare @x xml=N'';
select @x.query('(1,2,3,4)=1') --1 /* Returns True */
select @x.query('(1,2,3,4)!=1')--2 /* Returns True...how is this possible*/

for the first case the value is True because atomic value in left sequence is equal to 1(left atomic value)

But for second case also its True How is this possible i was expecting a false?

1 Answer 1

3

According to Comparison Expressions (XQuery):

When you are comparing two sequences by using general comparison operators and a value exists in the second sequence that compares True to a value in the first sequence, the overall result is True. Otherwise, it is False. For example, (1, 2, 3) = (3, 4) is True, because the value 3 appears in both sequences.

(1,2,3,4)=1 is equivalent to (1,2,3,4)=(1) in this case 1 exists in the first sequence so it's TRUE. Though for 2,3,4 it's FALSE BUT using the above rule if for at least one element condition is TRUE then OVERALL result is TRUE.

(1,2,3,4)!=1 is equivalent to (1,2,3,4)!=(1) we have 1 from the second sequence and it's compared TRUE (with !=) to the element of the first sequence 2 or 3,4 so we get overall result - TRUE.

More examples:

(1,2,3,4)>3  - TRUE (4 > 3)
(1,2,3,4)<3  - TRUE (1,2 <3)
(1,2,3,4)<5  - TRUE (1,2,3,4 < 5) 
(1,2,3,4)>5  - FALSE (There is no element from the first sequence > 5 ) 

So if for at least one element condition is TRUE then overall result is TRUE.

Think about it as OR:

(1,2,3,4)!=1 => (1!=1) OR (2!=3) OR (3!=3) OR (4!=3) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.