Maddy
Maddy

Reputation: 927

Select query from XML data

I am trying to get result from XML data but only getting a value of first node.

create table #temp(xmlString nvarchar(max))
insert into #temp (xmlString) values 
('<?xml version="1.0" ?><response status = "ERROR">
<error>Error1</error>
<error>Error2</error>
</response>')

I want a result :

Error1, Error2

Please help. Thanks

Upvotes: 0

Views: 442

Answers (2)

Maddy
Maddy

Reputation: 927

correct answer

select STUFF((select ',' +  x.c.value('.', 'nvarchar(max)') 
from (select cast(xmlString as xml) as data from #temp)
as t outer apply t.data.nodes('/response/error')
as x(c)for xml path('')), 1, 1, '') as Errors

Upvotes: 0

roman
roman

Reputation: 117380

select
    x.c.value('.', 'nvarchar(128)') as value
from (select cast(xmlString as xml) as data from temp) as t
    outer apply t.data.nodes('/response/error') as x(c)

SQL FIDDLE EXAMPLE

Upvotes: 3

Related Questions