SQL Server,每10个字符拆分列值

本文关键字:拆分 字符 10个 Server SQL | 更新日期: 2023-09-27 17:53:25

需要拆分每10个字符的列参考值,并需要显示每一行ID

我有这样的数据

ID       Reference
1        405123  ap41502   ag067156
2        a450912   d109456
3        4a4122   g567412  ol09123  pl212671 et61789
4        6t3450   41234a3   01a459014

所需的结果:

ID  Reference
1   405123  ap
1   41502   ag
1   067156
2   a450912   
2   d109456
3   4a4122   g
3   567412  ol
3   09123  pl2
3   12671 et61
3   789
4   6t3450   4
4   1234a3   0
4   1a459014

SQL Server,每10个字符拆分列值

您可以使用数字/计数表。交叉连接你的表到数字表

select  *, substring(t.Reference, (n.n * 10) + 1, 10)
from    yourtable t
    cross join numbers n
where   n.n >= 0
and     n.n < ceiling(len(t.Reference) / 10.0)
order by ID, n.n

注意:理货表应该包含一行0