开发者

How to returns the text() of an xml value with a space between the nodes value?

开发者 https://www.devze.com 2023-04-01 20:31 出处:网络
In SQL Server 2008 R2, Considering the following xml DECLARE @xml xml = \'<data><fr>Chambre standard</fr><en>Standard room</en></data>\';

In SQL Server 2008 R2,

Considering the following xml

DECLARE @xml xml = '<data><fr>Chambre standard</fr><en>Standard room</en></data>';

How can I return the following string:

Chambre standard Standard room
开发者_高级运维

Currently, I'm only able to return the string concatenated together without any space by using

SELECT @xml.query('//*/text()').value('.', 'varchar(max)')

Which return

Chambre standardStandard room

But I need to insert a space in there.

How could I insert a space between the node text?


You can use .nodes() to shred your XML.

select T.N.value('local-name(.)', 'varchar(128)') as Name,
       T.N.value('.', 'varchar(max)') as Value
from @xml.nodes('/data/*') as T(N);

Result:

Name  Value
fr    Chambre standard
en    Standard room

Then you can use for xml path('') to bring it back together.

select stuff((select ' '+T.N.value('.', 'varchar(max)')
              from @xml.nodes('/data/*') as T(N)
              for xml path(''), type).value('.', 'varchar(max)'), 1, 1, '')

Result:

(No column name)
Chambre standard Standard room
0

精彩评论

暂无评论...
验证码 换一张
取 消