Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql replace last characters in string if matched

I have a table that has some rogue tags that need replacing

The offending string ends <tr> and needs replacing with </table>

Not all record are affected, so I need to find these and then replace them

Our skills using Update Replace Where are limited as the characters are not unique within the string but their position is, ie the last 4 characters

Have tried using

UPDATE table
SET field
REPLACE (RIGHT(field,4),</table>)

but suspec this is over simplified (and also fails)

like image 645
user1589800 Avatar asked Aug 10 '12 09:08

user1589800


People also ask

How do I change the last letter of a string?

Use the String. replace() method to replace the last character in a string, e.g. const replaced = str.

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

try this:

UPDATE table
SET field=concat(left(field,length(field) -4),'</table>')
like image 79
Joe G Joseph Avatar answered Sep 21 '22 15:09

Joe G Joseph