2

I am using MS SQL Server 2012 and have the following XML structure:

<value name="parameter">
    <![CDATA[__na__]]>
    <![CDATA[1]]>
    <![CDATA[2]]>
    <![CDATA[3]]>
    <![CDATA[12]]>
</value>

and it is saved in the database as string in NVARCHAR(MAX) data type field.

When I cast this value as follows:

CAST(ColumnValue AS XML)

I get the following XML structure:

<value name="parameter">__na__12312</value>

Is there a way to divide the CDATA with commas for exmaple and get the following structure:

<value name="parameter">__na__,1,2,3,12</value>

In my real case the string length can vary and I am using only part of the whole XML, so replacing each ']]>' with ']]>,' does not sound as good solution. Is there an other way to do this?

4

1 回答 1

1

如果你不介意第一个字符带有逗号,你可以试试这个:

/*Loading test data*/
declare @xml varchar(8000)
set @xml = '
<value name="parameter">
    <![CDATA[__na__]]>
    <![CDATA[1]]>
    <![CDATA[2]]>
    <![CDATA[3]]>
    <![CDATA[12]]>
</value>'

/*the replace*/
set @xml= REPLACE(@xml,'<![CDATA[','<![CDATA[,' )
select CAST(@xml AS XML)

这将导致:

<value name="parameter">,__na__    ,1    ,2    ,3    ,12    </value>

此替换可确保您仅更改 CDATA 而不是其他 xml 参数或值,并且当您恢复值“参数”时,您只需忽略第一个字符

于 2013-10-23T14:43:13.327 回答