How to modify the xml in sql server

1:Checked the data_xml  saved in DB as nvarchar(1073741823).So need to convert the format to XML.
Firstly I used cast(data_xml AS XML)
But it reported the err of "XML parsing: line 1, character 56, unable to switch the encoding"

2:It is caused by the xml_data including the encoding="utf-8".So need to replace this.I tried below,it works
CAST(REPLACE(CAST(xml_data AS VARCHAR(MAX)), 'encoding="utf-8"',' ')AS XML)
Actually,if you want to change utf-8 to utf-16,you can REPLACE(CAST(xml_data AS VARCHAR(MAX)), 'encoding="utf-8"','encoding="utf-16"')

3:Then you can use xml_data.modify('replace'),I found below link declare this very clear.
https://www.cnblogs.com/zk-zhou/p/6389185.html

4:Attached with all the script I use

select id,CAST(REPLACE(CAST(data_xml AS VARCHAR(MAX)), 'encoding="utf-8"',' ')AS XML) as data_xml   into #t1 from 
@table_name(nolock)
where id=123

update #t1
set form_data_xml.modify('
            replace value of (/node1/node1_1/node1_1_number/text())[1]
            with "123456"
            ')

update olf1
set olf1.data_xml= cast (t.data_xml as VARCHAR(MAX))
from @table_name olf1
inner join #t1 t on t.id=olf1.id
drop table #t1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值