字符串行转列加循环的问题
con1
abcdefghijk
如何用循环的方法得到3行
con2
abc
def
ghi
------解决方案--------------------declare @con1 varchar(100)
declare @a table(a varchar(10))
declare @i int
set @con1= 'abcdefghijk '
while @i <len(@con1)/3
begin
insert @a select substring(@con1,@i*3+1,3)
set @i=@i+1
end
select * from @a
------解决方案--------------------declare @con1 table(a char(3))
declare @strs varchar(1000)
set @strs= 'abcdefghijk '
declare @i int
set @i=3
while @i <=len(@strs)
begin
insert into @con1
select substring(@strs,1,@i)
select @strs=substring(@strs,4,len(@strs)-3)
end
select * from @con1
------解决方案--------------------Declare @con1 Varchar(100)
Set @con1= 'abcdefghijk '
Select TOP 100 ID = Identity(Int, 0, 1) Into #T From SysColumns A, SysObjects B
Select
Substring (@con1,ID*3+1,3) As con2
From #T
Where ID <= Len(@con1) / 3
Drop table #T
--Result
/*
con2
abc
def
ghi
jk
*/