views:

1036

answers:

4

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
A: 

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

Manu
+1  A: 

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

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.

Mufaka
+3  A: 

I think 2 update calls should do

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

update VersionedFields
set Value = replace(value,'> </iframe>','</a>')
kristof
+3  A: 
update VersionedFields
set Value = replace(replace(value,'<iframe','<a>iframe'), '> </iframe>','</a>')

and you do it in a single pass.