Ana Ameer
Ana Ameer

Reputation: 681

Dynamically change values for SQL Server table?

I have a SQL Server table with the following column names:

filled with about 300 items. Now, the ImagePath column name is filled with different images for each entry, but they all have the same root folder in common (e.g. /project/images/1.jpg, /project/images/2.jpg, /project/images/3.jpg, etc.). I have been asked to move the images somewhere else, however, so now the root folder has changed (suppose the new location is /projects/project-a/images), and I'd like to know if there's a way of updating the ImagePath at once for all entries, as opposed to having to go through every single one of them manually. If possible, I'd like to avoid dropping and creating the table again.

Upvotes: 0

Views: 54

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82474

If the data type of the ImagePath column is char, varchar, nchar, or nvarchar, but NOT text or ntext, you can simply use the built in REPLACE method:

UPDATE tableName
SET ImagePath = REPLACE(ImagePath, '/project/images/', '/projects/project-a/images')

Upvotes: 4

Related Questions