How to replace a string in a SQL Server Table Column

SqlSql ServerDatabaseDatabase Administration

Sql Problem Overview


I have a table (SQL Sever) which references paths (UNC or otherwise), but now the path is going to change.

In the path column, I have many records and I need to change just a portion of the path, but not the entire path. And I need to change the same string to the new one, in every record.

How can I do this with a simple update?

Sql Solutions


Solution 1 - Sql

It's this easy:

update my_table
set path = replace(path, 'oldstring', 'newstring')

Solution 2 - Sql

UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')

Solution 3 - Sql

I tried the above but it did not yield the correct result. The following one does:

update table
set path = replace(path, 'oldstring', 'newstring') where path = 'oldstring'

Solution 4 - Sql

UPDATE CustomReports_Ta
SET vchFilter = REPLACE(CAST(vchFilter AS nvarchar(max)), '\\Ingl-report\Templates', 'C:\Customer_Templates')
where CAST(vchFilter AS nvarchar(max)) LIKE '%\\Ingl-report\Templates%'

Without the CAST function I got an error

Argument data type ntext is invalid for argument 1 of replace function.

Solution 5 - Sql

You can use this query

update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'

Solution 6 - Sql

all answers are great but I just want to give you a good example

select replace('this value from table', 'table',  'table but updated')

this SQL statement will replace the existence of the word "table" (second parameter) inside the given statement(first parameter) with the third parameter

the initial value is this value from table but after executing replace function it will be this value from table but updated

and here is a real example

UPDATE publication
SET doi = replace(doi, '10.7440/perifrasis', '10.25025/perifrasis')
WHERE doi like '10.7440/perifrasis%'

for example if we have this value

10.7440/perifrasis.2010.1.issue-1

it will become

10.25025/perifrasis.2010.1.issue-1

hope this gives you better visualization

Solution 7 - Sql

select replace(ImagePath, '~/', '../') as NewImagePath from tblMyTable 

> where "ImagePath" is my column Name.
"NewImagePath" is temporery > column Name insted of "ImagePath"
"~/" is my current string.(old > string)
"../" is my requried string.(new string)
> "tblMyTable" is my table in database.

Solution 8 - Sql

you need to replace path with the help of replace function.

update table_name set column_name = replace(column_name, 'oldstring', 'newstring')

here column_name refers to that column which you want to change.

Hope it will work.

Solution 9 - Sql

If target column type is other than varchar/nvarchar like text, we need to cast the column value as string and then convert it as:

update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'

Solution 10 - Sql

You also can replace large text for email template at run time, here is an simple example for that.

DECLARE @xml NVARCHAR(MAX)
SET @xml = CAST((SELECT [column] AS 'td','',        
        ,[StartDate] AS 'td'
         FROM [table] 
         FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
select REPLACE((EmailTemplate), '[@xml]', @xml) as Newtemplate 
FROM [dbo].[template] where id = 1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIralda MitroView Question on Stackoverflow
Solution 1 - SqlcjkView Answer on Stackoverflow
Solution 2 - SqlMarc GravellView Answer on Stackoverflow
Solution 3 - SqlCaesarView Answer on Stackoverflow
Solution 4 - SqlIgor BakayView Answer on Stackoverflow
Solution 5 - SqlNitika ChopraView Answer on Stackoverflow
Solution 6 - SqlBasheer AL-MOMANIView Answer on Stackoverflow
Solution 7 - SqlDurgesh PandeyView Answer on Stackoverflow
Solution 8 - Sqluser11441779View Answer on Stackoverflow
Solution 9 - Sqlkhichar.anilView Answer on Stackoverflow
Solution 10 - SqlShekhar PatelView Answer on Stackoverflow