Update a Column Value, Replacing Part of a String

Update a column value, replacing part of a string

UPDATE urls
SET url = REPLACE(url, 'domain1.example/images/', 'domain2.example/otherfolder/')

UPDATE and REPLACE part of a string

You don't need wildcards in the REPLACE - it just finds the string you enter for the second argument, so the following should work:

UPDATE dbo.xxx
SET Value = REPLACE(Value, '123', '')
WHERE ID <=4

If the column to replace is type text or ntext you need to cast it to nvarchar

UPDATE dbo.xxx
SET Value = REPLACE(CAST(Value as nVarchar(4000)), '123', '')
WHERE ID <=4

Update column, replace part of value

You could use the replace function:

UPDATE content
SET keywords = REPLACE(keywords, 'tekst', 'text')
WHERE keywords LIKE '%tekst%'

UPDATE and REPLACE part of a existing column value with new column value

UPDATE [tablename] SET [RequestName] = REPLACE(RequestName,'Victor','Victor-ID')

Replace some parts of string and update the column in STANDARD SQL

You are overusing parentheses, so something does not match.

Use this:

SET year = REPLACE(REPLACE(REPLACE(REPLACE(year, ' ', ''), 'X', ''), 'V', ''), 'I', '')

SQL update part of string

Replace it

update blist
set serial_no = replace(serial_no, 'abcd_', 'xyz_')
where city = 'US'
and serial_no like 'abcd%'

Demo

Update and Replace a start part of string from a column

Can you try this one:

UPDATE myTable
SET myColumn = '/1/2/3/' + REVERSE(SUBSTRING(REVERSE(myColumn),0,CHARINDEX('/',REVERSE(myColumn))))

It should insert /1/2/3/ in front and keep the part after last / from the original column value.

EDIT:

By the way, your query would also work if you removed SELECT FROM part.

UPDATE myTable
SET myColumn = REPLACE(myColumn, SUBSTRING(myColumn, 0, len(myColumn) - CHARINDEX('/',REVERSE(myColumn)) +1), '/1/2/3')

Problem is, your query with select returned all the rows from your table and tried to update your single row value with values from subquery. That's why you got an error.

MYSQL: Update a column value from a substring in another column?

Use SUBSTRING_INDEX:

UPDATE yourTable
SET link_Id = SUBSTRING_INDEX(Applicant, '_', -1);


Related Topics



Leave a reply



Submit