我可以在 t-sql 中进行查找/替换吗? [英] Can I do a find/replace in t-sql?

查看:48
本文介绍了我可以在 t-sql 中进行查找/替换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有一个xml列,我需要在每条记录中查找并替换一个标签值.

I basically have an xml column, and I need to find and replace one tag value in each record.

推荐答案

对于任何真实的事情,我都会使用 xpaths,但有时您只需要一个快速而肮脏的解决方案:

For anything real, I'd go with xpaths, but sometimes you just need a quick and dirty solution:

您可以使用 CAST 将该 xml 列转换为常规 varchar,然后进行常规替换.

You can use CAST to turn that xml column into a regular varchar, and then do your normal replace.

UPDATE xmlTable SET xmlCol = REPLACE( CAST( xmlCol as varchar(max) ), '[search]', '[replace]')

当您只需要运行快速查询来查找某些内容而不想处理 xpath 时,同样的技术也使搜索 XML 变得轻而易举.

That same technique also makes searching XML a snap when you need to just run a quick query to find something, and don't want to deal with xpaths.

SELECT * FROM xmlTable WHERE CAST( xmlCol as varchar(max) ) LIKE '%found it!%'

只是想稍微更新一下,如果您收到一条消息不可能将一个或多个字符从 XML 转换为目标排序规则,那么您只需要使用 nvarchar支持unicode.

Just want to update this a bit, if you get a message along the lines of Conversion of one or more characters from XML to target collation impossible, then you only need to use nvarchar which supports unicode.

CAST( xmlCol as nvarchar(max) )

这篇关于我可以在 t-sql 中进行查找/替换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆