Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Other SQL Server 2012 Topics
 Numeric Compare two xml nodes in SQL SERVER

Author  Topic 

Neeli
Starting Member

1 Post

Posted - 2013-10-21 : 03:06:37
I have a column XMLRECORD of XML type in a table called ABCD. My records are

<row id="100" xml:space="preserve">
<c9>400</c9>
<c10>4000</c10>
</row>


<row id="90" xml:space="preserve">
<c9>324</c9>
<c10>34</c10>
</row>
I am trying to compare the two nodes of this XMLRECORD using the query

SELECT XMLRECORD FROM ABCD WHERE XMLRECORD.exist(N'/row[c9/text() < c10/text()]') = 1

When executed in SQL SERVER it displays both the records doing the ASCII compare. How to force the SQL SERVER to go for a numeric compare?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-21 : 09:11:03
cast and then do the comparison.
See this illustration

declare @x table
( x xml
)
insert @x
values('<row id="100" xml:space="preserve">
<c9>400</c9>
<c10>4000</c10>
</row>'),
('<row id="90" xml:space="preserve">
<c9>324</c9>
<c10>34</c10>
</row>')

SELECT * FROM @x WHERE x.exist('/row[c9[1] cast as xs:integer? < c10[1] cast as xs:integer?]') = 1


output
-------------------------------------
x
-------------------------------------
<row id="100" xml:space="preserve">
<c9>400</c9>
<c10>4000</c10>
</row>



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -