dbTalk Databases Forums  

Decode XML Data Type

microsoft.public.sqlserver.xml microsoft.public.sqlserver.xml


Discuss Decode XML Data Type in the microsoft.public.sqlserver.xml forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Charles
 
Posts: n/a

Default Decode XML Data Type - 08-03-2010 , 05:45 PM






I have something like the following:

declare @msg xml

set @msg = '<Message><SubItem>Some Text</SubItem></Message>'

I'd like to be able to extract the value of the SubItem node, but I can't
find the syntax. It might be something like (I made this up):

SELECT x.SubItem
FROM @msg

and it would return the single row

SubItem
----------------
Some Text

Can anyone tell me what I should write?

TIA

Charles

Reply With Quote
  #2  
Old   
Dan Guzman
 
Posts: n/a

Default Re: Decode XML Data Type - 08-03-2010 , 08:17 PM






Quote:
declare @msg xml

set @msg = '<Message><SubItem>Some Text</SubItem></Message>'

I'd like to be able to extract the value of the SubItem node, but I can't
find the syntax. It might be something like (I made this up):

SELECT x.SubItem
FROM @msg

and it would return the single row

SubItem
----------------
Some Text
One method:

DECLARE @msg xml;
SET @msg = '<Message><SubItem>Some Text</SubItem></Message>';
SELECT @msg.query('/Message/SubItem[1]').value('.', 'varchar(100)') AS
SubItem;


--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

Reply With Quote
  #3  
Old   
Charles
 
Posts: n/a

Default Re: Decode XML Data Type - 08-04-2010 , 04:03 AM



Hi Dan

Thanks very much for the reply. That's exactly what I needed.

I also found that I can omit the [1] and it still works, perhaps because I
only have one element called SubItem?

Cheers

Charles


"Dan Guzman" <guzmanda (AT) nospam-online (DOT) sbcglobal.net> wrote

Quote:
declare @msg xml

set @msg = '<Message><SubItem>Some Text</SubItem></Message>'

I'd like to be able to extract the value of the SubItem node, but I can't
find the syntax. It might be something like (I made this up):

SELECT x.SubItem
FROM @msg

and it would return the single row

SubItem
----------------
Some Text

One method:

DECLARE @msg xml;
SET @msg = '<Message><SubItem>Some Text</SubItem></Message>';
SELECT @msg.query('/Message/SubItem[1]').value('.', 'varchar(100)') AS
SubItem;


--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/



Reply With Quote
  #4  
Old   
Dan Guzman
 
Posts: n/a

Default Re: Decode XML Data Type - 08-04-2010 , 07:12 AM



Quote:
I also found that I can omit the [1] and it still works, perhaps because I
only have one element called SubItem?
Only the first SubItem will be returned due to the ordinal specification.
If you omit the ordinal and have multiple SubItem elements, the text of all
of the SubItem elements will be concatenated. The example below uses the
XML nodes method as an alternative. This will shred SubItem elements
individually. You can also use this as alternative to the XML query method
I posted originally even if you have only one SubItem.

DECLARE @msg xml;
SET @msg = '<Message><SubItem>Some Text1</SubItem><SubItem>Some
Text2</SubItem></Message>';
SELECT SubItem.query('./text()')
FROM @msg.nodes('/Message/SubItem') AS Message(SubItem);

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

Reply With Quote
  #5  
Old   
Charles
 
Posts: n/a

Default Re: Decode XML Data Type - 08-04-2010 , 07:59 AM



That's very helpful, thanks Dan. I'll have an experiment with the two
methods and decide which is going to work best in my situation.

Cheers

Charles


"Dan Guzman" <guzmanda (AT) nospam-online (DOT) sbcglobal.net> wrote

Quote:
I also found that I can omit the [1] and it still works, perhaps because
I only have one element called SubItem?

Only the first SubItem will be returned due to the ordinal specification.
If you omit the ordinal and have multiple SubItem elements, the text of
all of the SubItem elements will be concatenated. The example below uses
the XML nodes method as an alternative. This will shred SubItem elements
individually. You can also use this as alternative to the XML query
method I posted originally even if you have only one SubItem.

DECLARE @msg xml;
SET @msg = '<Message><SubItem>Some Text1</SubItem><SubItem>Some
Text2</SubItem></Message>';
SELECT SubItem.query('./text()')
FROM @msg.nodes('/Message/SubItem') AS Message(SubItem);

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.