问题
I have a string which is of the form
Text I Want to Discard (TEXT I WANT)
I only want the part of the string contained in brackets. How do I go about getting this substring?
回答1:
How about this:
select substring(col, charindex('(', col), len(col))
from yourtable;
See SQL Fiddle with Demo
Or check for both brackets. This gets the location of the opening bracket ( and then returns the length of the string between the opening and closing bracket:
select substring(col, charindex('(', col), charindex(')', col) - charindex('(', col) +1)
from yourtable;
See SQL Fiddle with Demo
回答2:
Try the below... it works...
DECLARE @c varchar(100)
SET @c = 'Text I Want to Discard (TEXT I WANT)'
SET @c = Replace(Replace(@c,')','_'),'(','_');
SELECT SUBSTRING(
@c,
CHARINDEX('_', @c) + 1,
LEN(@c) - CHARINDEX('_', @c) - CHARINDEX('_', REVERSE(@c))
)
来源:https://stackoverflow.com/questions/14503909/extracting-part-of-a-string-using-sql