我有以下两列:
Repay_Aount | Repay_Ref | 150.063829.07 | t21q1p - 20210529t21xyn - 20210428 |
---|---|
160.1216502429.49 | t21ymg - 20210628t21gvx - 20210531t21q1p - 20210529 |
115.9104.2826001461.47 | t21jk9 - 20210731t21jg1 - 20210731t21qzp - 20210724t21ymg - 20210628 |
您需要一个拆分函数,它返回每个子字符串的顺序位置。从SQL Server 2022开始,STRING_SPLIT()
支持可选的enable_ordinal
参数
对于早期版本,基于json的方法是一种选择。其思想是将存储的文本转换为有效的JSON数组(115.9104.2826001461.47
转换为["115.9","104.28","2600","1461.47"]
),并使用OPENJSON()
和默认模式解析该数组。结果是一个包含key
,value
,type
列的表,其中key
列保存指定数组中元素的索引。
SELECT RA.[value] AS [SPLIT_REPAY_AMOUNT], RR.[value] AS [SPLIT_REPAY_ref]
FROM AA_BILL_DETAILS
CROSS APPLY STRING_SPLIT(REPAY_AMOUNT, N'', 1) RA
CROSS APPLY STRING_SPLIT(REPAY_ref, N'', 1) RR
WHERE RA.[ordinal] = RR.[ordinal]
SQL Server 2016+:SELECT RA.[value] AS [SPLIT_REPAY_AMOUNT], RR.[value] AS [SPLIT_REPAY_ref]
FROM AA_BILL_DETAILS
CROSS APPLY OPENJSON(CONCAT('["', REPLACE(REPAY_AMOUNT, N'', '","'), '"]')) RA
CROSS APPLY OPENJSON(CONCAT('["', REPLACE(REPAY_REF, N'', '","'), '"]')) RR
WHERE RA.[key] = RR.[key]