Quiz: Comparisons in XQuery vs T-SQL

I have the following two
statements.  What is the difference?  What would you expect
the results to be?  Are you surprised by what you get?

select 1
where convert(xml, '<foo>bar</foo>').value('/foo[1]', 'nvarchar(4)') = 'Bar'

select 1
where convert(xml, '<foo>bar</foo>').exist('/foo[.="Bar"]') = 1

Comments (2)

  1. Dean Harding says:

    Well (assuming the syntax is correct and all that, I’ve never used the XML datatype before), XQuery is case-sensitive, so the second one won’t select anything. However, the first one may or may not select anything, depending on whether you’ve got your database set to use a case-sensitive collation or not.

  2. Last week I threw out this simple little quiz.&amp;nbsp; Here is the answer.

    The first query will return…

Skip to main content