SQL 如何分列

来源:百度知道 编辑:UC知道 时间:2024/06/17 14:37:31
我现在处理一个数据库,表中有一列的情况是,例如:500|6222|2220
每条竖线间的数的位数不定,可能是个位,或则是千位等等,同时有数的数量也不一定,可能是3组数或则4组或则更多,2220|3561|56565|65656。
用什么程序能把他分列,按|分成各列,有几组数就分成几列,有什么办法??

declare @表1 table (id int,数据 varchar(10))
insert into @表1
select 1,'a b c' union all
select 2,'c a n' union all
select 3,'s c v'

select id,数据1=parsename(replace(数据,' ','.'),3),
数据2=parsename(replace(数据,' ','.'),2),
数据3=parsename(replace(数据,' ','.'),1) from @表1
/*
id 数据1 数据2 数据3
------------------
1a b c
2c a n
3s c v
*/

--模拟数据
create table #yourtable(ID int,Content varchar(4000))
insert into #yourtable(ID,Content)
select 1,'22|5000|3000'
union all select 2,'1|35|200|2'
union all select 3,'802|22'
union all select 4,'213|354|2002|22|500'

--实际操作SQL
declare @sql nvarchar(4000),@i int
set @i=1
while exists(select 1 from #yourtable where Content<>'')
begin
set @sql='alter table #yourtable add Data'+conve