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
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2101 Visits: 1009
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 Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53673 Visits: 16674
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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45291 Visits: 14686
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 (115K reputation)SSC Guru (115K reputation)SSC Guru (115K reputation)SSC Guru (115K reputation)SSC Guru (115K reputation)SSC Guru (115K reputation)SSC Guru (115K reputation)SSC Guru (115K reputation)

Group: General Forum Members
Points: 115288 Visits: 21446
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 Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53673 Visits: 16674
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
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2101 Visits: 1009
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