tags:

views:

90

answers:

2

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 :

<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>

I want to take just the languages part of this xml and put it in another XML variable.

How do i do that ?

+1  A: 

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
astander
+1  A: 

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
Pratap .R