在数据库中搜索并替换部分字符串

时间:2022-01-17 16:49:16

I need to replace all iframe tags, stored as nvarchar in my database. I can find the entries using the following sql-question:

我需要替换所有作为nvarchar存储在数据库中的iframe标记。我可以使用下面的sql问题找到条目:

SELECT * FROM databasename..VersionedFields WHERE Value LIKE '%<iframe%'

Say I want to replace the following code segment:

假设我想替换以下代码段:

code before iframe <iframe src="yadayada"> </iframe> code after iframe

With this:

用这个:

code before iframe <a>iframe src="yadayada"</a> code after iframe

6 个解决方案

#1


82  

I think 2 update calls should do

我认为两个更新电话就可以了

update VersionedFields
set Value = replace(value,'<iframe','<a><iframe')

update VersionedFields
set Value = replace(value,'> </iframe>','</a>')

#2


98  

You can do it with an UPDATE statement setting the value with a REPLACE

可以使用UPDATE语句设置替换值

UPDATE
    Table
SET
    Column = Replace(Column, 'find value', 'replacement value')
WHERE
    xxx

You will want to be extremely careful when doing this! I highly recommend doing a backup first.

你在做这件事的时候要特别小心!我强烈建议先做一个备份。

#3


13  

update VersionedFields
set Value = replace(replace(value,'<iframe','<a>iframe'), '> </iframe>','</a>')

and you do it in a single pass.

你一次就搞定了。

#4


4  

I was just faced with a similar problem. I exported the contents of the db into one sql file and used TextEdit to find and replace everything I needed. Simplicity ftw!

我刚刚遇到了一个类似的问题。我将db的内容导出到一个sql文件中,并使用TextEdit来查找和替换所需的所有内容。简单增值!

#5


0  

I would consider writing a CLR replace function with RegEx support for this kind of string manipulation.

我将考虑为这种字符串操作编写一个支持RegEx的CLR替换函数。

#6


-1  

Update database and Set fieldName=Replace (fieldName,'FindString','ReplaceString')

更新数据库并设置fieldName=Replace (fieldName,'FindString','ReplaceString')

#1


82  

I think 2 update calls should do

我认为两个更新电话就可以了

update VersionedFields
set Value = replace(value,'<iframe','<a><iframe')

update VersionedFields
set Value = replace(value,'> </iframe>','</a>')

#2


98  

You can do it with an UPDATE statement setting the value with a REPLACE

可以使用UPDATE语句设置替换值

UPDATE
    Table
SET
    Column = Replace(Column, 'find value', 'replacement value')
WHERE
    xxx

You will want to be extremely careful when doing this! I highly recommend doing a backup first.

你在做这件事的时候要特别小心!我强烈建议先做一个备份。

#3


13  

update VersionedFields
set Value = replace(replace(value,'<iframe','<a>iframe'), '> </iframe>','</a>')

and you do it in a single pass.

你一次就搞定了。

#4


4  

I was just faced with a similar problem. I exported the contents of the db into one sql file and used TextEdit to find and replace everything I needed. Simplicity ftw!

我刚刚遇到了一个类似的问题。我将db的内容导出到一个sql文件中,并使用TextEdit来查找和替换所需的所有内容。简单增值!

#5


0  

I would consider writing a CLR replace function with RegEx support for this kind of string manipulation.

我将考虑为这种字符串操作编写一个支持RegEx的CLR替换函数。

#6


-1  

Update database and Set fieldName=Replace (fieldName,'FindString','ReplaceString')

更新数据库并设置fieldName=Replace (fieldName,'FindString','ReplaceString')