Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XQuery: Get the value only, and not the child node values


XQuery: Get the value only, and not the child node values

Author
Message
aaron.reese
aaron.reese
Mr or Mrs. 500
Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)

Group: General Forum Members
Points: 589 Visits: 898
Hi Guys,

If I have the following XML


<ROOT>
<SPECIALNEEDS>
INCLUDE
<NEED>
BLIND
</NEED>
<NEED>
BRAILLE AUDIO
</NEED>
</SPECIALNEEDS>
</ROOT>


How do I get the value "INCLUDE" only.

xmltext.value('(ROOT/SPECIALNEEDS)[1]', nvarchar(max)) is giving me all the value contents (Include Blind Braille Audio)
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478


DECLARE @xml XML =
'<ROOT>
<SPECIALNEEDS>
INCLUDE
<NEED>
BLIND
</NEED>
<NEED>
BRAILLE AUDIO
</NEED>
</SPECIALNEEDS>
</ROOT>'


SELECT TD.D.value('./text()[1]','Varchar(400)')
FROM @xml.nodes('/ROOT/SPECIALNEEDS') AS TD(D)




Please note the whitespace preserved on the left of INCLUDE, as it's part of the node text.
You can LTRIM it or make sure you XML formed appropriately eg:

'<ROOT>
<SPECIALNEEDS>INCLUDE
<NEED>
BLIND
</NEED>
<NEED>
BRAILLE AUDIO
</NEED>
</SPECIALNEEDS>
</ROOT>'



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
aaron.reese
aaron.reese
Mr or Mrs. 500
Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)Mr or Mrs. 500 (589 reputation)

Group: General Forum Members
Points: 589 Visits: 898
Thanks Eugene, That was really helpful.

Just because I am anal about these things ;-)

How would you write the select statement if the data was in a #temptable with a column of type XML
Mark Cowne
Mark Cowne
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: 2095 Visits: 22800
Use CROSS APPLY



DECLARE @xml XML =
'<ROOT>
<SPECIALNEEDS>
INCLUDE
<NEED>
BLIND
</NEED>
<NEED>
BRAILLE AUDIO
</NEED>
</SPECIALNEEDS>
</ROOT>'

SELECT @xml AS xmlCol
INTO #temptable

SELECT TD.D.value('./text()[1]','Varchar(400)')
FROM #temptable
CROSS APPLY xmlCol.nodes('/ROOT/SPECIALNEEDS') AS TD(D)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




carlosaamaral
carlosaamaral
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 1049
LIVING AND LEARNING!!!!!!! :-D
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