Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace last occurrence of a substring in MYSQL?

Tags:

database

mysql

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?

like image 328
Jeets Avatar asked Mar 21 '13 09:03

Jeets


People also ask

How do you replace last occurrence?

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.

How do you find the last occurrence of a character in a string in MySQL?

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.

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

This is shorter and more readable:

SELECT TRIM(TRAILING 'Corp' FROM 'American Corp National Corp')
like image 89
Mirko Avatar answered Sep 19 '22 20:09

Mirko