How to replace a string in a SQL Server Table Column
Asked Answered
S

10

402

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?

Sandhi answered 2/5, 2009 at 9:43 Comment(0)
M
680

It's this easy:

update my_table
set path = replace(path, 'oldstring', 'newstring')
Megalocardia answered 2/5, 2009 at 9:45 Comment(2)
I'd typically add where path like '%oldstring%' if there was a lot of data.Gyratory
where condition make sense because if I have 50 rows in table and if I am replacing 10 rows with replace function it affects all 50 rows, even though it replaces 10 rows if you dont have where condition. But if you have where condition like mentioned in above comment it only affects 10 rows.Thigmotaxis
C
149
UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')
Connection answered 2/5, 2009 at 9:47 Comment(1)
sorry, not to be nitpicky after ten years but it's unclear from the answer if foo is being replaced or bar (sorry again)Owlish
E
32

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'
Effective answered 1/3, 2012 at 23:26 Comment(2)
This totally defeats the purpose of using the replace method. You can accomplish the same thing like this: update table set path='newstring' where path='oldstring';Zeigler
perhaps you meant where path like '%oldstring%'?Odilia
H
18
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.

Hyperostosis answered 9/12, 2012 at 14:25 Comment(0)
P
14

You can use this query

update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'
Publicize answered 27/4, 2017 at 10:10 Comment(0)
A
11

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

Attribution answered 24/7, 2017 at 16:1 Comment(0)
O
8

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.

Owades answered 10/5, 2019 at 13:30 Comment(0)
B
6
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.

Biarritz answered 30/3, 2016 at 14:53 Comment(0)
B
4

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'
Brecher answered 21/7, 2015 at 14:9 Comment(0)
C
0

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
Crossarm answered 29/3, 2017 at 4:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.