Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract right string between last two parentheses

I need to extract the values inside the last parentheses of each row. Column Name is what I have and column ID is what I need.

Notice that row 2 has two set of parentheses and that one row has no parentheses.

![enter image description here

I tried using this script:

SUBSTRING(name,CHARINDEX('(',name)+1 ,CHARINDEX(')',name)-CHARINDEX('(',name)-1) 

The script returns error message Invalid length parameter passed to the LEFT or SUBSTRING function. because of the the third row has no parentheses.

like image 481
user716255 Avatar asked Mar 22 '26 21:03

user716255


1 Answers

For your sample data, this should do what you want:

select replace(right(name, charindex('(', reverse(name) + '(') - 1), ')', '')
like image 168
Gordon Linoff Avatar answered Mar 24 '26 11:03

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!