How can I replace the last occurrence of a substring with blank string in MYSQL?I could not find any such direct function in MYSQL
String: "American Corp National Corp"
Search String: "Corp"
Expected output: "American Corp National"
Could anyone suggest?
To replace the last occurrence of a character in a string: Use the lastIndexOf() method to get the last index of the character. Call the substring() method twice, to get the parts of the string before and after the character to be replaced. Add the replacement character between the two calls to the substring method.
Learn MySQL from scratch for Data Science and Analytics To get the first n characters of string with MySQL, use LEFT(). To get the last n char of string, the RIGHT() method is used 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.
This is shorter and more readable:
SELECT TRIM(TRAILING 'Corp' FROM 'American Corp National Corp')
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