SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML Query help


XML Query help

Author
Message
sam 55243
sam 55243
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1755 Visits: 987
Hi,

I have below XML and i need to fetch the data for First set of token tag


<tokens>
<token>
<typeid>1</typeid>
<p1>0.15</p1>
</token>

<token>
<typeid>1</typeid>
<op>+</op>
<p1>2.568</p1>
</token>
</tokens>



Output i need for first token set as highlighted above Typeid as 1 and p1 as 0.15 i dont want to read/parse the entire xml only first set of tag <token> here in this case. using tsql. Please help

Thanks
Sam
Thom A
Thom A
SSC-Forever
SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)

Group: General Forum Members
Points: 46230 Visits: 15719
This what you're looking for?

DECLARE @XML xml;
SET @XML =
'<tokens>
<token>
<typeid>1</typeid>
<p1>0.15</p1>
</token>
<token>
<typeid>1</typeid>
<op>+</op>
<p1>2.568</p1>
</token>
</tokens>';

SELECT X.T.value('(token/typeid/text())[1]','int') AS typedid,
X.T.value('(token/p1/text())[1]','decimal(6,3)') AS p1
FROM @XML.nodes('/tokens') X(T);




Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
drew.allen
drew.allen
SSC-Dedicated
SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)

Group: General Forum Members
Points: 37104 Visits: 13737
Since you're only looking for ONE row, there is no reason to use the relatively expensive .nodes() function.

SELECT @XML.value('(/tokens/token/typeid/text())[1]', 'int') AS typeidid,
@XML.value('(/tokens/token/p1/text())[1]', 'decimal(6,3)') AS p1id


Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94894 Visits: 20696
drew.allen - Wednesday, January 3, 2018 8:44 AM
Since you're only looking for ONE row, there is no reason to use the relatively expensive .nodes() function.

SELECT @XML.value('(/tokens/token/typeid/text())[1]', 'int') AS typeidid,
@XML.value('(/tokens/token/p1/text())[1]', 'decimal(6,3)') AS p1id


Drew

Good point Drew, the nodes() function will parse and then reconstruct the xml, quite costly!
Cool

Thom A
Thom A
SSC-Forever
SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)

Group: General Forum Members
Points: 46230 Visits: 15719
Eirikur Eiriksson - Wednesday, January 3, 2018 8:54 AM
drew.allen - Wednesday, January 3, 2018 8:44 AM
Since you're only looking for ONE row, there is no reason to use the relatively expensive .nodes() function.

SELECT @XML.value('(/tokens/token/typeid/text())[1]', 'int') AS typeidid,
@XML.value('(/tokens/token/p1/text())[1]', 'decimal(6,3)') AS p1id


Drew

Good point Drew, the nodes() function will parse and then reconstruct the xml, quite costly!
Cool


Hadn't considered that, in all honestly. Thanks Drew. Smile


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
sam 55243
sam 55243
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1755 Visits: 987
Thom A - Wednesday, January 3, 2018 9:14 AM
Eirikur Eiriksson - Wednesday, January 3, 2018 8:54 AM
drew.allen - Wednesday, January 3, 2018 8:44 AM
Since you're only looking for ONE row, there is no reason to use the relatively expensive .nodes() function.

SELECT @XML.value('(/tokens/token/typeid/text())[1]', 'int') AS typeidid,
@XML.value('(/tokens/token/p1/text())[1]', 'decimal(6,3)') AS p1id


Drew

Good point Drew, the nodes() function will parse and then reconstruct the xml, quite costly!
Cool


Hadn't considered that, in all honestly. Thanks Drew. Smile

Thanks for Quick help..

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search