T-SQL cut XML into pieces
I have a table
create table Profiles
(id int,
xml_data xml
)
that has an xml column and for each row i have the same xml that looks like this :
<P开发者_JS百科rofile>
<personalInfo>
<contactInfo>
<firstName>John</firstName>
<lastName>Doe</lastName>
<address>22 Park Avenue</address>
<address2 />
<city>Guilford</city>
<state>CT</state>
<zip>11221</zip>
</contactInfo>
</personalInfo>
<Languages>
<Language> English </Language>
<Language> French </Language>
<Language> German </Language>
<Language> Hungarian </Language>
</Languages>
</Profile>
I want to take just the languages part of this xml and put it in another XML variable.
How do i do that ?
does this work? The query uses the first row's xml_data
declare @x2 xml
select top 1 @x2=xml_data.query('Profile/Languages')
from Profiles
Have a look at something like this
DECLARE @xml XML
SET @xml =
'<Profile>
<personalInfo>
<contactInfo>
<firstName>John</firstName>
<lastName>Doe</lastName>
<address>22 Park Avenue</address>
<address2 />
<city>Guilford</city>
<state>CT</state>
<zip>11221</zip>
</contactInfo>
</personalInfo>
<Languages>
<Language> English </Language>
<Language> French </Language>
<Language> German </Language>
<Language> Hungarian </Language>
</Languages>
</Profile>'
SELECT @xml.query('Profile/Languages')
You can also do this for a column for a table.
SELECT *,
xml_data.query('Profile/Languages')
FROM @Table
精彩评论