使用sql提取字符串的一部分 [英] Extracting part of a string using sql

查看:32
本文介绍了使用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

这篇关于使用sql提取字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆