目前我有一个列(Details),它是一个分隔字符串。我需要能够解析和匹配批次ID值与来自另一个表的CX.STRING_4 (批次ID)值。
SQL 2008r2
Details Column String: ex
Request to prepare method. Context: Site: | Factory: | Unite ID: | Batch ID:0000123456 | Product Name: |对一种方法有什么想法?例如,内部连接/解析函数
发布于 2018-12-06 04:00:44
提取它的方法是将substring与charindex和其他一些操纵器一起使用。
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)您可以使用相同的语法以几种方式与之匹配。
发布于 2018-12-06 04:10:49
假设在"Batch ID:“之后没有空格,并且在Batch ID的实际值之后有一个空格,您可以这样做:
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))https://stackoverflow.com/questions/53639476
复制相似问题