Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql - Way to update portion of a string?

I'm looking for a way to update just a portion of a string via MySQL query.

For example, if I have 10 records all containing 'string' as part of the field value (i.e., 'something/string', 'something/stringlookhere', 'something/string/etcetera', is there a way to change 'string' to 'anothervalue' for each row via one query, so that the result is 'something/anothervalue', 'something/anothervaluelookhere', 'something/string/etcetera', is there a way to change 'anothervalue'

like image 624
n00b0101 Avatar asked Dec 09 '09 20:12

n00b0101


People also ask

How do I UPDATE a text field in MySQL?

Change table_name and field to match your table name and field in question: UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE INSTR(field, 'foo') > 0; REPLACE (string functions)

How do you UPDATE a substring in SQL?

For MySQL change, LEN to LENGTH , and the second parameter of SUBSTRING is the starting offset, so it should be one more than the number of characters to strip. So, to remove 4 characters, this value would be 5.

How do I change the first character of a string in MySQL?

You could simply use: UPDATE customers_basket SET products_id=CONCAT('S', SUBSTRING(products_id FROM 2)); i.e.: Instead of replacing the initial "U" with an "S", simply start with an "S" and copy the remaining characters.

How do I remove a specific character from a string in MySQL?

Remove characters from string using TRIM() TRIM() function is used to remove any character/ whitespace from the start/ end or both from a string.


1 Answers

I think this should work:

UPDATE table SET field = REPLACE(field, 'string', 'anothervalue') WHERE field LIKE '%string%'; 
like image 168
Kaleb Brasee Avatar answered Oct 07 '22 07:10

Kaleb Brasee