'1,2,3,6-8,10'转换为'1,2,3,6,7,8,10'

declare @str nvarchar(100),@count int,@num1 int,@num2 int
select @str='1,2,3,4-6,7,8-15,24-30'
select @str=','+@str+','
while len(@str)-len(replace(@str,'-',''))>0
begin
select @count=charindex('-',@str)
print charindex(',',reverse(substring(@str,1,@count-1)))-1
--获取-前面的值
select @num1=reverse(substring(reverse(substring(@str,1,@count-1)),1,charindex(',',reverse(substring(@str,1,@count-1)))-1))
--获取-后面的值
select @num2=substring(@str,@count+1,charindex(',',(substring(@str,@count+1,len(@str))))-1)

if @num2-@num1=1
set @str=stuff(@str,@count,1,',')
else
set @str=stuff(@str,charindex('-',@str),1,','+cast((@num1+1)as nvarchar(100))+'-')
end
print reverse(stuff(reverse(stuff(@str,1,1,'')),1,1,''))

你可能感兴趣的:(转换)