Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a string in a SQL Server Table Column

I have a table (SQL Sever) which references paths (UNC or otherwise), but now the path is going to change.

In the path column, I have many records and I need to change just a portion of the path, but not the entire path. And I need to change the same string to the new one, in every record.

How can I do this with a simple update?

like image 813
Iralda Mitro Avatar asked May 02 '09 09:05

Iralda Mitro


People also ask

How do you change a string value in SQL?

UPDATE Syntax Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!


2 Answers

It's this easy:

update my_table set path = replace(path, 'oldstring', 'newstring') 
like image 62
cjk Avatar answered Oct 21 '22 01:10

cjk


UPDATE [table] SET [column] = REPLACE([column], '/foo/', '/bar/') 
like image 39
Marc Gravell Avatar answered Oct 21 '22 01:10

Marc Gravell