sql - 两个斜杠之间的 T-SQL 子字符串提取数据

标签 sql sql-server

我正在尝试为我正在处理的项目提取 T-SQL 中的字符串的一部分。

示例:

/客户/AAA/某事/某事

/客户/BBBB/某事/某事

我专门尝试提取 AAA 或 BBB,它们的字符数量不一致。

最佳答案

使用 CHARINDEX 和 SUBSTRING 尝试以下操作。

drop table #a
create table #a (d varchar(100))

insert into #a (d)
    values   ('/Clients/AAA/Something/Something/')
            ,('/Clients/bbbbb/Something/Something/')

select  d       as [OriginalData]
        ,charindex('/', d, charindex('/', d, 0)+1) as [SecondSlash]
        ,charindex('/', d, charindex('/', d, charindex('/', d, 0)+1)+1)  as [ThirdSlash]
        ,SUBSTRING(d    -- Value
                    , charindex('/', d, charindex('/', d, 0)+1)+1  -- Startpoint (SecondSlash) + 1
                    , charindex('/', d, charindex('/', d, charindex('/', d, 0)+1)+1) - charindex('/', d, charindex('/', d, 0)+1)-1) as [Extract]
                                        -- Endpoint (ThirdSlash - SecondSlash - 1)
from #a

这有点困惑,只会返回第二个和第三个斜杠之间的文本,但应该相当快。

关于sql - 两个斜杠之间的 T-SQL 子字符串提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53604519/

相关文章:

sql - 提高 PostgreSQL 聚合性能

sql - SQL 中是否需要 ID 列?

sql-server - 将查询结果传递给存储过程

sql-server - 不能使用 LONG 数据类型

sql-server - SQL Server : String vs Binary?

sql-server - 如何构造 SQL 语句

mysql - 在 SQL 查询中选择连续组中的最小值 (MySQL)

mysql - 也许如何从选择中选择

sql - 转换为日期时间 - 带有日期的 DateTime 列和带有时间的 nvarchar 列

c# - 为什么它不插入到sql表中?