gpt4 book ai didi

SQL Server : parsing two values from one varchar with variable length

转载 作者:行者123 更新时间:2023-12-02 03:57:25 25 4
gpt4 key购买 nike

我的表中有一个 varchar 列。在本专栏中,结构是一致的,但长度可能略有不同。

以下是我可能要处理的一些示例。请注意这些值如何符合相同的模式,但其中包含的数值(保单编号和两个帐号)的长度可能略有不同。

DECLARE @historyDescription1 VARCHAR(50)
DECLARE @historyDescription2 VARCHAR(50)
DECLARE @historyDescription3 VARCHAR(50)

SET @historyDescription1 = 'Policy ZZ 998560-17 transferred from Account 0028397267 to Account 192135980'
SET @historyDescription2 = 'Policy ZZ 9985601-17-00 transferred from Account 128397267 to Account 792135980'
SET @historyDescription3 = 'Policy ZZ 998560789-17-00 transferred from Account 228397267 to Account 0192135980'

对于每条记录,我需要选择第一个帐号和第二个帐号。总会有两个人在场。

类似...

SELECT [first parsed value]+','+[second parsed value]
FROM myTable
WHERE [...]

这样我就能得到像......这样的结果

0028397267,192135980  
128397267,792135980
228397267,0192135980

我一直在尝试使用 SUBSTRINGCHARINDEXLEFTRIGHT 但无济于事。任何帮助表示赞赏。

最佳答案

我尝试过这个,它似乎适用于您的示例值:

select substring(hist, pos1 + len1, pos2 - pos1 - len1 -1), substring(hist, pos2 + len2, lenhist - pos2 - len2 +1)
from (
select
pos1 = charindex('from Account', hist)
, pos2 = charindex('to Account', hist)
, len1 = len('from Account')
, len2 = len('to Account')
, lenhist = len(hist)
, *
from t
) x

我使用派生表来保持表达式简单。

一个demo can be found here .

关于SQL Server : parsing two values from one varchar with variable length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43355163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com