sql 服务器中单列中多列的逗号分隔值
Comma separated values of multiple columns in single column in sql server
我有一个 table 包含三列 Id、Errorcode、ErrorDescription,其中 errorcode 和 errordescription 列中有逗号分隔值
这里我需要连接第2列和第3列的值,例如第2列的第一个值-第3列的第一个值等等用逗号(,)分隔
示例:实际 Table
Id
Errorcode
ErrorDescription
1
204,201,33
Invalid Object,Out Of Range,Invalid Format
2
21,44
FileInvalid,Invalid date
3
20
Invalid parse
所需输出:
Id
Error
1
204-Invalid Object, 201-Out Of Range, 33-Invalid Format
2
21-FileInvalid, 44-Invalid date
3
20-Invalid parse
..来自 sqlserver 2017 ..fiddle
select *
from
(
values
(1, '204,201,33', 'Invalid Object,Out Of Range,Invalid Format'),
(2, '21,44', 'FileInvalid,Invalid date'),
(3, '20', 'Invalid parse')
) as t(Id, Errorcode, ErrorDescription)
cross apply
(
select string_agg(c.value+'-'+e.value, ', ') within group (order by cast(c.[key] as int)) as error
from openjson('["'+replace(string_escape(t.Errorcode, 'json'), ',', '","')+'"]') as c
join openjson('["'+replace(string_escape(t.ErrorDescription, 'json'), ',', '","')+'"]') as e on c.[key] = e.[key]
) as e;
..sqlserver 2016..fiddle
select *
from
(
values
(1, '204,201,33', 'Invalid Object,Out Of Range,Invalid Format'),
(2, '21,44', 'FileInvalid,Invalid date'),
(3, '20', 'Invalid parse')
) as t(Id, Errorcode, ErrorDescription)
cross apply
(
select stuff ((
select ', '+c.value+'-'+e.value
from openjson('["'+replace(string_escape(t.Errorcode, 'json'), ',', '","')+'"]') as c
join openjson('["'+replace(string_escape(t.ErrorDescription, 'json'), ',', '","')+'"]') as e on c.[key] = e.[key]
order by cast(c.[key] as int)
for xml path(''), type).value('text()[1]', 'nvarchar(max)'), 1, 2, '') as error
) as e;
我有一个 table 包含三列 Id、Errorcode、ErrorDescription,其中 errorcode 和 errordescription 列中有逗号分隔值
这里我需要连接第2列和第3列的值,例如第2列的第一个值-第3列的第一个值等等用逗号(,)分隔
示例:实际 Table
Id | Errorcode | ErrorDescription |
---|---|---|
1 | 204,201,33 | Invalid Object,Out Of Range,Invalid Format |
2 | 21,44 | FileInvalid,Invalid date |
3 | 20 | Invalid parse |
所需输出:
Id | Error |
---|---|
1 | 204-Invalid Object, 201-Out Of Range, 33-Invalid Format |
2 | 21-FileInvalid, 44-Invalid date |
3 | 20-Invalid parse |
..来自 sqlserver 2017 ..fiddle
select *
from
(
values
(1, '204,201,33', 'Invalid Object,Out Of Range,Invalid Format'),
(2, '21,44', 'FileInvalid,Invalid date'),
(3, '20', 'Invalid parse')
) as t(Id, Errorcode, ErrorDescription)
cross apply
(
select string_agg(c.value+'-'+e.value, ', ') within group (order by cast(c.[key] as int)) as error
from openjson('["'+replace(string_escape(t.Errorcode, 'json'), ',', '","')+'"]') as c
join openjson('["'+replace(string_escape(t.ErrorDescription, 'json'), ',', '","')+'"]') as e on c.[key] = e.[key]
) as e;
..sqlserver 2016..fiddle
select *
from
(
values
(1, '204,201,33', 'Invalid Object,Out Of Range,Invalid Format'),
(2, '21,44', 'FileInvalid,Invalid date'),
(3, '20', 'Invalid parse')
) as t(Id, Errorcode, ErrorDescription)
cross apply
(
select stuff ((
select ', '+c.value+'-'+e.value
from openjson('["'+replace(string_escape(t.Errorcode, 'json'), ',', '","')+'"]') as c
join openjson('["'+replace(string_escape(t.ErrorDescription, 'json'), ',', '","')+'"]') as e on c.[key] = e.[key]
order by cast(c.[key] as int)
for xml path(''), type).value('text()[1]', 'nvarchar(max)'), 1, 2, '') as error
) as e;