Search and replace part of a string in a 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%' 

Let's say I want to replace the following code segment:

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

Wherein:

 code before iframe <a>iframe src="yadayada"</a> code after iframe 
+61
sql sql-server tsql
Mar 03 '09 at 9:45
source share
6 answers

I think 2 update calls should do

 update VersionedFields set Value = replace(value,'<iframe','<a><iframe') update VersionedFields set Value = replace(value,'> </iframe>','</a>') 
+82
Mar 03 '09 at 10:01
source share

You can do this with an UPDATE statement that sets a value using REPLACE

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

You will be very careful with this! I highly recommend backing up first.

+94
Mar 03 '09 at 10:00
source share
 update VersionedFields set Value = replace(replace(value,'<iframe','<a>iframe'), '> </iframe>','</a>') 

and you do it in one go.

+13
Mar 03 '09 at 13:15
source share

I ran into a similar problem. I exported the contents of db to a single sql file and used TextEdit to find and replace everything I need. Simplicity ftw!

+4
Apr 13 '13 at 3:37 on
source share

I would consider writing a CLR replacement function using RegEx for this kind of string manipulation.

0
Mar 03 '09 at 9:54
source share

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

-one
Jul 28 '17 at 13:19
source share



All Articles