Search code examples
mysqlstringstrip

strip the string in MySQL based on specific characters around


I have a column with values that I need to strip. All the strings have the same pattern like the following:

Name
IIT | Cosmo (949446)
MIIT | World (949446)
HNTY | Name (576376)

I only need to grab the value that is in the middle. I was able to get this far:

SUBSTRING_INDEX(`Name`, '|', -1) as `New_Name`

Which gives me back Cosmo (949446), World (949446), Name (576376) The desired output is: Cosmo, World, Name.

Thank you.


Solution

  • You can use substring_index on your output one more time.

    SELECT TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX("IIT | Cosmo (949446)", '|', -1),'(',1))