SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Other SQL Server 2012 Topics
 Numeric Compare two xml nodes in SQL SERVER
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Neeli
Starting Member

1 Posts

Posted - 10/21/2013 :  03:06:37  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 10/21/2013 :  09:11:03  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000