简体   繁体   中英

Update the contents of SQL Server table column

I want to add a single character to contents of my SQL Server table.

In the table I have a column HighResolutionImage . The existing content of one of the rows that column is

http://s3-eu-west-1.amazonaws.com/coliseumimages/high_0f34869c3b5745d4.jpg

I want to update all the rows to have https in that HighResoltionImage column.

How do I update the content of a table in such a way that I update all the over 1000 rows affected?

I tried updating it one by one but it will take time because I have over 1000 rows to edit.

I like STUFF() for this:

UPDATE dbo.tableName 
  SET HighResolutionImage = STUFF(HighResolutionImage, 5, 0, 's')
  WHERE HighResolutionImage LIKE 'http://%';

While in this specific case there should only be one instance of http:// in the string, STUFF() can extend to other scenarios where you only want to replace a specific instance. If you'd rather use REPLACE() then you can do as HoneyBadger's comment:

UPDATE dbo.tableName 
  SET   HighResolutionImage = REPLACE
       (HighResolutionImage, 'http://', 'https://')
  WHERE HighResolutionImage LIKE 'http://%';

Just keep in mind that it will replace every instance of http:// so may not be the right solution in other scenarios that don't involve URLs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM