使用sql提取字符串的一部分

使用sql提取字符串的一部分

问题描述:

我有一个形式为

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?

这个怎么样:

select substring(col, charindex('(', col), len(col))  
from yourtable;

参见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;

参见SQL Fiddle with Demo