解析定界数据并匹配单个值

时间:2018-12-05 19:31:25

标签: sql sql-server-2008

当前,我有一个列(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: |

对方法有任何想法吗?例如内部联接/解析功能

2 个答案:

答案 0 :(得分:0)

substringcharindex和其他一些操纵器一起使用的提取方式。

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)

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

答案 1 :(得分:0)

假设“批次ID:”后没有空格,而批次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))