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'
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)
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.
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.
Remove characters from string using TRIM() TRIM() function is used to remove any character/ whitespace from the start/ end or both from a string.
I think this should work:
UPDATE table SET field = REPLACE(field, 'string', 'anothervalue') WHERE field LIKE '%string%';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With