字符串拆分-1.1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caishu1995/article/details/85201175

准备工作

    1、准备一个变量用于存要被拆分的字符串

declare @oldWorkflowId varchar(100)
set @oldWorkflowId = '3,5'
print @oldWorkflowId      --打印看看

拆分

declare @i int -- ,所处位置
declare @s int -- 当前遍历的位置
declare @test TABLE (c1 varchar(100)) --结果集合

set @i=1
set @s=1
while(@i>0)
	begin    
		set @i=charindex(',',@oldWorkflowId, @s) --更新@i位置
        --插入数据
		if(@i>0)
			begin
				INSERT INTO @test(c1) VALUES(substring(@oldWorkflowId,@s,@i-@s)) 
			end   
		else 
			begin
				INSERT INTO @test(c1) VALUES(substring(@oldWorkflowId,@s,len(@oldWorkflowId)-@s+1))
			end
		set @s = @i + 1   
	end
select * from @test -- 查询结果

猜你喜欢

转载自blog.csdn.net/caishu1995/article/details/85201175