Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL xml: get xml values into CSV?

Author  Topic 

mittalpa
Starting Member

2 Posts

Posted - 2009-02-04 : 17:45:36
Hi

In SQL 2005, I am trying to get a csv strin like "1212,1122,1123,1124" out of following xml.

<group>
<id>1212</id>
<id>1122</id>
<id>1123</id>
<id>1124</id>
</group>

Please advise how it can be done?

Thanks
Pankaj

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-05 : 01:05:18
[code]check this

DECLARE @r XML

SET @r = '
<group>
<id>1212</id>
<id>1122</id>
<id>1123</id>
<id>1124</id>
</group>'

select stuff((
SELECT ','+
t.n.value('text()[1]', 'VARCHAR(100)')
FROM @r.nodes('/*/*') AS t(n) for xml path('')),1,1,'')
[/code]
Go to Top of Page
   

- Advertisement -