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?
You can use REPLACE in an UPDATE statement.
Data modification side of DML language in T-SQL includes three statements used for modifying data in SQL Server and those are: INSERT, UPDATE, and DELETE.
If you'd like to replace a substring with another string, simply use the REPLACE function. This function takes three arguments: The string to change (which in our case was a column). The substring to replace.
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%'
UPDATE users SET picture = REPLACE(picture, 'http://servera.host.com/', 'http://serverb.host.com/') WHERE picture LIKE 'http://servera.host.com/%';
I'm including more of the string because I'd worry about 'fixing' an image named 'somethingserverasomething.jpg'. I might also think about having a base_url table and just storing image file names in users, but that's not the question you asked ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With