Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a simple 'Find and Replace" in MsSQL?

Tags:

sql

sql-server

Question is pretty self explanitory. I want to do a simple find and replace, like you would in a text editor on the data in a column of my database (which is MsSQL on MS Windows server 2003)

like image 663
Jiaaro Avatar asked Sep 12 '08 13:09

Jiaaro


People also ask

How do you replace a word in a table in SQL?

SQL Server REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive. Tip: Also look at the STUFF() function.

How do you perform a replace in SQL?

The basic syntax of replace in SQL is: REPLACE(String, Old_substring, New_substring); In the syntax above: String: It is the expression or the string on which you want the replace() function to operate.

How do I replace multiple characters in a string in SQL Server?

Using the REPLACE() function will allow you to change a single character or multiple values within a string, whether working to SELECT or UPDATE data.

How do I find and replace text in all stored procedures in SQL Server?

Select all the objects (you can multi-select from this window, which is pretty much the only purpose of the Object Explorer Details window) and right click, choosing to script as DROP and CREATE. You can now do a search/replace on this, replacing all you need in one go before executing it.


2 Answers

The following query replace each and every a character with a b character.

UPDATE      YourTable SET      Column1 = REPLACE(Column1,'a','b') WHERE      Column1 LIKE '%a%' 

This will not work on SQL server 2003.

like image 75
SQLMenace Avatar answered Oct 07 '22 07:10

SQLMenace


like so:

BEGIN TRANSACTION;  UPDATE table_name   SET column_name=REPLACE(column_name,'text_to_find','replace_with_this');  COMMIT TRANSACTION; 

Example: Replaces <script... with <a ... to eliminate javascript vulnerabilities

BEGIN TRANSACTION; UPDATE testdb SET title=REPLACE(title,'script','a'); COMMIT TRANSACTION; 
like image 25
Jiaaro Avatar answered Oct 07 '22 06:10

Jiaaro