Search code examples
sqlmysqlstringreplacesql-update

Replace a substring within a column value for all rows with an SQL UPDATE query


Here's a sample table:

name       |   picture

John S.    |   http://servera.host.com/johns.png
Linda B.   |   http://servera.host.com/lindab.png
...

Let's say there are several hundred more records.

Let's also say we moved servers from "servera" to "serverb".

Would it be possible to go into this table with one query to rename the content in the column "picture" for every record to read the correct server name?


Solution

  • T-SQL:

    update TBL 
       set picture = Replace(picture, 'servera', 'serverb') 
     where picture like '%servera%'
    

    Oracle:

    update TBL 
       set picture = replace(picture, 'servera', 'serverb') 
     where picture like '%servera%'
    

    MySQL:

    update TBL 
       set picture = REPLACE(picture, 'servera', 'serverb') 
     where picture like '%servera%'