问题描述
我有一个带有特定模式的字符串:
I have a string with a specific pattern:
23;chair,red [$3]
即,一个数字后跟一个分号,然后一个名称后跟一个左方括号.
i.e., a number followed by a semicolon, then a name followed by a left square bracket.
假设分号 ;
始终存在并且左方括号 [
始终存在于字符串中,我如何提取 之间(且不包括)之间的文本>;
和 [
在 SQL Server 查询中?谢谢.
Assuming the semicolon ;
always exists and the left square bracket [
always exists in the string, how do I extract the text between (and not including) the ;
and the [
in a SQL Server query? Thanks.
推荐答案
结合 SUBSTRING()
、LEFT()
和 CHARINDEX()
代码>函数.
Combine the SUBSTRING()
, LEFT()
, and CHARINDEX()
functions.
SELECT LEFT(SUBSTRING(YOUR_FIELD,
CHARINDEX(';', YOUR_FIELD) + 1, 100),
CHARINDEX('[', YOUR_FIELD) - 1)
FROM YOUR_TABLE;
这假设您的字段长度永远不会超过 100,但您可以在必要时使用 LEN()
函数使其更智能地解决这个问题.我没有打扰,因为那里已经发生了足够多的事情,而且我没有可以测试的实例,所以我只是盯着我的括号等.
This assumes your field length will never exceed 100, but you can make it smarter to account for that if necessary by employing the LEN()
function. I didn't bother since there's enough going on in there already, and I don't have an instance to test against, so I'm just eyeballing my parentheses, etc.
这篇关于如何在 SQL Server 中提取这个特定的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!