0

I have a table (about 160k rows) with a column called paths. In that column there are paths like:

"\\ab.local\folder1\folder2\folder3\folder_x"

"\\ab.local\folderA\"

The length of the paths differ.

What I would like to do is to replace only the "ab" before the .local in to "cd" and leave the rest untouched.

I have been told to use a replace function, but somehow I don't get it to work the way I want to.

I am looking for the right syntax to do this.

Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
Renegade
  • 3
  • 3
  • 3
    Please show is your query which isn't working so we can help you correct it... – Dale K Aug 08 '19 at 06:39
  • Also put different examples. Does it always start with \\? Is it always ab.? If not, is it always 2 chars and a dot? Etc. – EzLo Aug 08 '19 at 06:40

3 Answers3

0

If the part to replace is on a fixed position with a fixed length you could use STUFF, like so:

UPDATE yourTable SET paths = STUFF(paths, 3, 2, 'cd')

This replaces two characters in paths beginning at position 3 with cd

0
Declare @oldval as varchar(30) = '\\ab.local\' ;
Declare @newval as varchar(30) = '\\cd.local\' ; 

update yourtable set yourfield  = replace(yourfield,@oldval ,@newval) where yourfield like @oldval + '%'
Wilhelm
  • 196
  • 10
0

Solved Reference : https://stackoverflow.com/a/814551/6923146

Hope it's perfect for your solution

UPDATE my_table
SET columnName = replace(columnName, 'oldstring', 'newstring')
WHERE columnName like '%oldstring%'

For example:

UPDATE my_table
SET columnName = replace(columnName, '\ab.', '\ab.')
WHERE columnName like '%\ab.%'