在一个数据表中 表结构及记录如下:
bh mc
001 x/y/z32s(50/25/25)
002 x/y45s(65/35)
003 x40s
.........................根据以上表生成另外一个表结构如下: bh mc js
001 x 50
001 y 25
001 z 25
002 x 65
002 y 35
003 x 100 ......................
bh mc
001 x/y/z32s(50/25/25)
002 x/y45s(65/35)
003 x40s
.........................根据以上表生成另外一个表结构如下: bh mc js
001 x 50
001 y 25
001 z 25
002 x 65
002 y 35
003 x 100 ......................
645s
x40s
是什么意思?
drop table tb
gocreate table tb(bh varchar(10),mc varchar(50))
insert into tb(bh,mc) values('001', 'x/y/z32s(50/25/25)')
insert into tb(bh,mc) values('002', 'x/y45s(65/35)')
insert into tb(bh,mc) values('003', 'x40s' )
goalter table tb add mc1 varchar(50)
alter table tb add mc2 varchar(50)
goupdate tb
set mc1 = left(mc , charindex('(',mc) -1),
mc2 = substring(mc , charindex('(' , mc) + 1 , charindex(')' , mc) - charindex('(' , mc) - 1)
where charindex('(' , mc) > 0SELECT TOP 8000 id = identity(int,1,1)
INTO # FROM syscolumns a, syscolumns b SELECT
id = identity(int,1,1),
A.bh,
mc1 = SUBSTRING(A.mc1, B.ID, CHARINDEX('/', A.mc1 + '/', B.ID) - B.ID)
into test1
FROM tb A, # B
WHERE SUBSTRING('/' + A.mc1, B.ID, 1) = '/'
ORDER BY 1,2SELECT
id = identity(int,1,1),
A.bh,
mc2 = SUBSTRING(A.mc2, B.ID, CHARINDEX('/', A.mc2 + '/', B.ID) - B.ID)
into test2
FROM tb A, # B
WHERE SUBSTRING('/' + A.mc2, B.ID, 1) = '/'
ORDER BY 1,2select a.bh , left(a.mc1,1) mc1 , b.mc2 from test1 a,test2 b where a.id = b.iddrop table tb,#,test1,test2/*
bh mc1 mc2
---------- ---- --------------------------------------------------
001 x 50
001 y 25
001 z 25
002 x 65
002 y 35(所影响的行数为 5 行)
*/
drop table tb
gocreate table tb(bh varchar(10),mc varchar(50))
insert into tb(bh,mc) values('001', 'x/y/z32s(50/25/25)')
insert into tb(bh,mc) values('002', 'x/y45s(65/35)')
insert into tb(bh,mc) values('003', 'x40s' )
goalter table tb add mc1 varchar(50)
alter table tb add mc2 varchar(50)
goupdate tb
set mc1 = left(mc , charindex('(',mc) -1),
mc2 = substring(mc , charindex('(' , mc) + 1 , charindex(')' , mc) - charindex('(' , mc) - 1)
where charindex('(' , mc) > 0SELECT TOP 8000 id = identity(int,1,1)
INTO # FROM syscolumns a, syscolumns b SELECT
id = identity(int,1,1),
A.bh,
mc1 = SUBSTRING(A.mc1, B.ID, CHARINDEX('/', A.mc1 + '/', B.ID) - B.ID)
into test1
FROM tb A, # B
WHERE SUBSTRING('/' + A.mc1, B.ID, 1) = '/'
ORDER BY 1,2SELECT
id = identity(int,1,1),
A.bh,
mc2 = SUBSTRING(A.mc2, B.ID, CHARINDEX('/', A.mc2 + '/', B.ID) - B.ID)
into test2
FROM tb A, # B
WHERE SUBSTRING('/' + A.mc2, B.ID, 1) = '/'
ORDER BY 1,2select a.bh , left(a.mc1,1) mc1 , b.mc2 from test1 a,test2 b where a.id = b.id
union all
select bh , left(mc,1) as mc1 , mc2 = 100 from tb where charindex('(' , mc) <= 0drop table tb,#,test1,test2/*
bh mc1 mc2
---------- ---- -----------
001 x 50
001 y 25
001 z 25
002 x 65
002 y 35
003 x 100(所影响的行数为 6 行)
*/