Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml query

Status
Not open for further replies.

simian336

Programmer
Sep 16, 2009
723
US
I am kind of new to XML data. I have a regular text field with xml data like....

Attribs

<Variables><Var><N>Email Address</N><V>tom@test.com</V></Var></Variables>
<Variables><Var><N>Email Address</N><V>Bill@test.com</V></Var></Variables>
<Variables><Var><N>Email Address</N><V>Jim@test.com</V></Var></Variables>
<Variables><Var><N>Email Address</N><V>Julie@test.com</V></Var></Variables>

I found where I can convert the field to xml by

select cast(attribs as xml) as test
from userinfo

I want a query to return just the email addresses.

tom@test.com
Bill@test.com
Jim@test.com
Julie@test.com

Thanks

Simi
 
Thanks slayer but that example uses a cross apply that would return 2 values for each row, like this....

<Person>
<Name>Simon</Name>
<Age>20</Age>
<Skills>
<Skill>Cooking</Skill>
<Skill>Cleaning</Skill>
</Skills>
</Person>

My example is a little simple I just have 2 straight fields... But I am trying to make it work....

Simi
 
simian,

You show 4 rows of sample data. Is that in 4 different rows in the database, or is this in one varchar(max) column (all four rows in the same xml data within a single row of the table)?

Code:
Declare @Temp Table(attribs varchar(max))

insert into @Temp Values('<Variables><Var><N>Email Address</N><V>tom@test.com</V></Var></Variables>')
insert into @Temp Values('<Variables><Var><N>Email Address</N><V>Bill@test.com</V></Var></Variables>')
insert into @Temp Values('<Variables><Var><N>Email Address</N><V>Jim@test.com</V></Var></Variables>')
insert into @Temp Values('<Variables><Var><N>Email Address</N><V>Julie@test.com</V></Var></Variables>')

Select Cast(attribs as xml).value('(/Variables/Var/V)[1]','varchar(100)') as test
From @Temp


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hey George,

Each row is different row in the table.

Simi
 
Perfect George, I responed before I tried your code.

Thanks

Simi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top