首先,让我说这就是为什么你不应该在一个字段中首先有逗号分隔的数据。没有简单有效的方法来处理它。
也就是说,您可以使用递归查询拆分字符串并从中获取数字:
with split as
(
select
item = cast('' as varchar(max)),
source = cast('20,0, 5,,^24,0, 0,,^26,0, 0,,^281,0, 0,,^34,0, 2,,^48,0, 2,,^44,0, 2,,^20,0, 10,,^20,5, 5,,^379,1, 1,,^26,1, 2,,^32,0, 1,,^71,0, 2,,^' as varchar(max))
union all
select
item = substring(source, 1, charindex(',,', source)),
source = substring(source, charindex(',,', source) + 2, 10000)
from split
where source > ''
)
select substring(item, 1, charindex(',', item) -1)
from split
where item > ''
结果:
20
^24
^26
^281
^34
^48
^44
^20
^20
^379
^26
^32
^71