首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析分隔数据,匹配单值

解析分隔数据,匹配单值
EN

Stack Overflow用户
提问于 2018-12-06 03:31:25
回答 2查看 43关注 0票数 1

目前我有一个列(Details),它是一个分隔字符串。我需要能够解析和匹配批次ID值与来自另一个表的CX.STRING_4 (批次ID)值。

SQL 2008r2

代码语言:javascript
复制
Details Column String: ex
Request to prepare method. Context: Site: | Factory: | Unite ID: | Batch ID:0000123456 | Product Name: |

对一种方法有什么想法?例如,内部连接/解析函数

EN

回答 2

Stack Overflow用户

发布于 2018-12-06 04:00:44

提取它的方法是将substringcharindex和其他一些操纵器一起使用。

代码语言:javascript
复制
declare @str varchar(max) = 'Request to prepare method. Context: Site: | Factory: | Unite ID: | Batch ID:0000123456 | Product Name: |'

select substring(@str,charindex('Batch ID',@str),charindex('|',substring(@str,charindex('Batch ID:',@str),99)) - 1)

您可以使用相同的语法以几种方式与之匹配。

票数 0
EN

Stack Overflow用户

发布于 2018-12-06 04:10:49

假设在"Batch ID:“之后没有空格,并且在Batch ID的实际值之后有一个空格,您可以这样做:

代码语言:javascript
复制
declare @str varchar(255) = 'Request to prepare method. Context: Site: | Factory: | Unite ID: | Batch ID:0000123456 | Product Name: |'

select @str
     -- Start of "Batch ID:" string
    ,charindex('Batch ID:', @str)
     -- Start of the actual Batch ID
    ,charindex('Batch ID:', @str) + 9 
     -- position of the space following the Batch ID
    ,charindex(' ', @str, charindex('Batch ID:', @str) + 9)
     -- length of the Batch ID
    ,charindex(' ', @str, charindex('Batch ID:', @str) + 9) - (charindex('Batch ID:', @str) + 9)
     -- pull it all together to extract the actual Batch ID
    ,substring(@str, charindex('Batch ID:', @str) + 9, charindex(' ', @str, charindex('Batch ID:', @str) + 9) - (charindex('Batch ID:', @str) + 9))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53639476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档