How to find a substring and replace it?

I have the following lines:

Name Url name1 http://foo.com/this/that name6 http://that.net/hello name2 http://foo.com/hello/world name3 http://foo.com/world/hello name4 http://hello.com/this/that 

I need to write a query that will change every foo.com to hello.com .

Any ideas?

+6
sql sql-server tsql
source share
5 answers

Take a look at the REPLACE () function: http://msdn.microsoft.com/en-us/library/ms186862.aspx . You should be able to use this in an UPDATE statement to achieve the desired result.

+5
source share
 UPDATE <table name> SET Url = REPLACE (Url, "foo.com" , "hello.com") 
+6
source share

Check replace :

 update table set url = replace(url, 'foo.com', 'hello.com') 
+4
source share
 UPDATE table SET Url = REPLACE(url, 'foo.com', 'hello.com') 
+3
source share
 UPDATE table SET Url = REPLACE(url, 'http://foo.com/', 'http://hello.com/') 

it's safer !!

+1
source share

All Articles