Here is the string :'(a+b)+(x/y)*1000′
from that string i want to get ‘(x/y)’ meaning i want the part that contains the division to check later if denominator <> 0 to avoid division by zero.
The string formula can vary but divisions are always between parenthesis.
How can i achieve that in sql ?
Bits that it appears you already have (based on a comment you made)…
'/'=CHARINDEX('/', yourString)')'=CHARINDEX(')', yourString, CHARINDEX('/', yourString) + 1)The position of the
(is a little different, as you need to search backwards. So you need to reverse the string. And so you also need to change the starting position.CHARINDEX('(', REVERSE(yourString), LEN(yourString) - CHARINDEX('/', yourString) + 2)Which give the position from the right hand side.
LEN(yourString) - position + 1give the position from the left hand side.Add that all together and you get a very long formula…