Zooking Zooking - 2 months ago 11
SQL Question

Search and replace part of string in database

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

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

Answer

I think 2 update calls should do

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

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