SQL获取第二个和第三个下划线之间的字符串

SQL获取第二个和第三个下划线之间的字符串,sql,Sql,我需要从表中的列中提取特定字符串作为SSIS包的一部分 该列内容的格式与此TST_AB1_ABC123456_测试类似 我需要获取第二个和第三个之间的字符串,例如ABC123456,而不需要对包进行太多更改,因此如果可能,我宁愿在1个SQL命令中执行该操作 我尝试了使用SUBSTRING、REVERSE和CHARINDEX的几种不同方法,但不知道如何仅获取该字符串。如果列值总是由4部分组成,则可以像这样使用函数 DECLARE @MyString VARCHAR(100) SET @MyStr

我需要从表中的列中提取特定字符串作为SSIS包的一部分

该列内容的格式与此TST_AB1_ABC123456_测试类似

我需要获取第二个和第三个之间的字符串,例如ABC123456,而不需要对包进行太多更改,因此如果可能,我宁愿在1个SQL命令中执行该操作


我尝试了使用SUBSTRING、REVERSE和CHARINDEX的几种不同方法,但不知道如何仅获取该字符串。

如果列值总是由4部分组成,则可以像这样使用函数

DECLARE @MyString VARCHAR(100)

SET @MyString = 'TST_AB1_ABC123456_TEST';

SELECT PARSENAME(REPLACE(@MyString, '_', '.'), 2)

如果列值始终有4个部分,则可以使用如下函数

DECLARE @MyString VARCHAR(100)

SET @MyString = 'TST_AB1_ABC123456_TEST';

SELECT PARSENAME(REPLACE(@MyString, '_', '.'), 2)

使用基本字符串函数:

SELECT
    SUBSTRING(col,
              CHARINDEX('_', col, CHARINDEX('_', col) + 1) + 1,
              CHARINDEX('_', col, CHARINDEX('_', col, CHARINDEX('_', col) + 1) + 1) -
                  CHARINDEX('_', col, CHARINDEX('_', col) + 1) - 1)
FROM yourTable;
在notes格式中,上述对子字符串的调用表示:

SELECT
    SUBSTRING(<your column>,
              <starting at one past the second underscore>,
              <for a length of the number of characters in between the 2nd and 3rd
                   underscore>)
FROM yourTable;

在其他数据库(如Postgres和Oracle)上,有子字符串索引和正则表达式函数可以更优雅地处理上述问题。实际上,较新版本的SQL Server有一个STRING_SPLIT函数,可以在此处使用,但它不维护结果部分的顺序。

使用基本字符串函数:

SELECT
    SUBSTRING(col,
              CHARINDEX('_', col, CHARINDEX('_', col) + 1) + 1,
              CHARINDEX('_', col, CHARINDEX('_', col, CHARINDEX('_', col) + 1) + 1) -
                  CHARINDEX('_', col, CHARINDEX('_', col) + 1) - 1)
FROM yourTable;
在notes格式中,上述对子字符串的调用表示:

SELECT
    SUBSTRING(<your column>,
              <starting at one past the second underscore>,
              <for a length of the number of characters in between the 2nd and 3rd
                   underscore>)
FROM yourTable;

在其他数据库(如Postgres和Oracle)上,有子字符串索引和正则表达式函数可以更优雅地处理上述问题。实际上,较新版本的SQL Server有一个STRING_SPLIT函数,可以在此处使用,但它不维护结果部分的顺序。

您也可以使用交叉应用来完成此操作。我添加了一个where子句,以确保不会因为没有3个下划线的字符串而导致错误

with your_table as (select 'TST_AB1_ABC123456_TEST' as txt1)

select txt1, txt2
from your_table t1
where txt1 like '%_%_%_%'
cross apply (select charindex( '_', txt1) as i1) t2 -- locate the 1st underscore
cross apply (select charindex( '_', txt1, (i1 + 1)) as i2 ) t3 -- then the 2nd
cross apply (select charindex( '_', txt1, (i2 + 1)) as i3 ) t4 -- then the 3rd
cross apply (select substring( txt1,(i2+1), (i3-i2-1)) as txt2) t5 -- between 2nd & 3rd
输出


您也可以使用交叉应用来执行此操作。我添加了一个where子句,以确保不会因为没有3个下划线的字符串而导致错误

with your_table as (select 'TST_AB1_ABC123456_TEST' as txt1)

select txt1, txt2
from your_table t1
where txt1 like '%_%_%_%'
cross apply (select charindex( '_', txt1) as i1) t2 -- locate the 1st underscore
cross apply (select charindex( '_', txt1, (i1 + 1)) as i2 ) t3 -- then the 2nd
cross apply (select charindex( '_', txt1, (i2 + 1)) as i3 ) t4 -- then the 3rd
cross apply (select substring( txt1,(i2+1), (i3-i2-1)) as txt2) t5 -- between 2nd & 3rd
输出


你能分享你的示例代码吗?你使用的是哪种数据库管理系统?许多产品都有自己的字符串函数…你能分享你的示例代码吗?你使用的是哪种dbms?许多产品都有自己的字符串函数。。。