select f1=left(f1,charindex('+',f1)-1),f2 from tb where charindex('+',f1)>0 union all select f1=substring(f1,charindex('+',f1)+1,len(f1)-charindex('+',f1)),f2 from tb where charindex('+',f1)>0 union all select f1,f2 from tb where charindex('+',f1)=0
还有点问题。 我那个f1 可以是 B+C+D+E... 也可是单的。
if object_id('pubs..tb') is not null drop table tb gocreate table tb(f1 varchar(10),f2 int) insert into tb(f1,f2) values('B+C',1) insert into tb(f1,f2) values('B+C',2) goselect left(f1,charindex('+',f1)-1) f1 , f2 from tb union all select substring(f1,charindex('+',f1)+1 , len(f1) - charindex('+',f1)) f1 , f2 from tbdrop table tb/* f1 f2 ---------- ----------- B 1 B 2 C 1 C 2(所影响的行数为 4 行) */
--如果个数,不定,需要临时表.if object_id('pubs..tb') is not null drop table tb gocreate table tb(f1 varchar(10),f2 int) insert into tb(f1,f2) values('B+C',1) insert into tb(f1,f2) values('B+C',2) go-- 建立一个辅助的临时表就可以了 SELECT TOP 8000 id = identity(int,1,1) INTO # FROM syscolumns a, syscolumns b SELECT f1 = SUBSTRING(tb.f1, B.ID, CHARINDEX('+', tb.f1 + '+', B.ID) - B.ID) , tb.f2 FROM tb, # B WHERE SUBSTRING('+' + tb.f1, B.id, 1) = '+' ORDER BY 1,2 GODROP TABLE tb,#/* f1 f2 ---------- ----------- B 1 B 2 C 1 C 2(所影响的行数为 4 行)*/
if object_id('pubs..tb') is not null drop table tb gocreate table tb(f1 varchar(10),f2 int) insert into tb(f1,f2) values('B+C+D+E',1) insert into tb(f1,f2) values('B+C',2) insert into tb(f1,f2) values('B',3) go-- 建立一个辅助的临时表就可以了 SELECT TOP 8000 id = identity(int,1,1) INTO # FROM syscolumns a, syscolumns b SELECT f1 = SUBSTRING(tb.f1, B.ID, CHARINDEX('+', tb.f1 + '+', B.ID) - B.ID) , tb.f2 FROM tb, # B WHERE SUBSTRING('+' + tb.f1, B.id, 1) = '+' ORDER BY 1,2 GODROP TABLE tb,#/* f1 f2 ---------- ----------- B 1 B 2 B 3 C 1 C 2 D 1 E 1(所影响的行数为 7 行) */
谢谢 还想多问下。 原表。库存型的。不是我设计的,我说的别人不听,没办法。 类型表 producttype producttypeid,producttypename 1 A+B+C+D 2 A+B+C 3 B+C .. 5 A 6 B 7 C 8 D ....出库主表 deliverymain deliverymainid producttypeid 1 1 明细 deliverydetail deliverydetailid deliverymainid num 1 1 100 2 1 200现在要求出每个单独产品的出库数量。 即上面写的。 A 100 B 100 C 100 D 100 A 200 B 200 C 200 D 200 解决了另发贴感谢 实在是不好做
from tb
where charindex('+',f1)>0
union all
select f1=substring(f1,charindex('+',f1)+1,len(f1)-charindex('+',f1)),f2
from tb
where charindex('+',f1)>0
union all
select f1,f2
from tb
where charindex('+',f1)=0
我那个f1 可以是 B+C+D+E...
也可是单的。
drop table tb
gocreate table tb(f1 varchar(10),f2 int)
insert into tb(f1,f2) values('B+C',1)
insert into tb(f1,f2) values('B+C',2)
goselect left(f1,charindex('+',f1)-1) f1 , f2 from tb
union all
select substring(f1,charindex('+',f1)+1 , len(f1) - charindex('+',f1)) f1 , f2 from tbdrop table tb/*
f1 f2
---------- -----------
B 1
B 2
C 1
C 2(所影响的行数为 4 行)
*/
drop table tb
gocreate table tb(f1 varchar(10),f2 int)
insert into tb(f1,f2) values('B+C',1)
insert into tb(f1,f2) values('B+C',2)
go-- 建立一个辅助的临时表就可以了
SELECT TOP 8000 id = identity(int,1,1)
INTO # FROM syscolumns a, syscolumns b
SELECT
f1 = SUBSTRING(tb.f1, B.ID, CHARINDEX('+', tb.f1 + '+', B.ID) - B.ID) ,
tb.f2
FROM tb, # B
WHERE SUBSTRING('+' + tb.f1, B.id, 1) = '+'
ORDER BY 1,2
GODROP TABLE tb,#/*
f1 f2
---------- -----------
B 1
B 2
C 1
C 2(所影响的行数为 4 行)*/
drop table tb
gocreate table tb(f1 varchar(10),f2 int)
insert into tb(f1,f2) values('B+C+D+E',1)
insert into tb(f1,f2) values('B+C',2)
insert into tb(f1,f2) values('B',3)
go-- 建立一个辅助的临时表就可以了
SELECT TOP 8000 id = identity(int,1,1)
INTO # FROM syscolumns a, syscolumns b
SELECT
f1 = SUBSTRING(tb.f1, B.ID, CHARINDEX('+', tb.f1 + '+', B.ID) - B.ID) ,
tb.f2
FROM tb, # B
WHERE SUBSTRING('+' + tb.f1, B.id, 1) = '+'
ORDER BY 1,2
GODROP TABLE tb,#/*
f1 f2
---------- -----------
B 1
B 2
B 3
C 1
C 2
D 1
E 1(所影响的行数为 7 行)
*/
还想多问下。
原表。库存型的。不是我设计的,我说的别人不听,没办法。
类型表 producttype
producttypeid,producttypename
1 A+B+C+D
2 A+B+C
3 B+C
..
5 A
6 B
7 C
8 D
....出库主表 deliverymain
deliverymainid producttypeid
1 1
明细 deliverydetail
deliverydetailid deliverymainid num
1 1 100
2 1 200现在要求出每个单独产品的出库数量。
即上面写的。
A 100
B 100
C 100
D 100
A 200
B 200
C 200
D 200
解决了另发贴感谢
实在是不好做
--通过函数分拆
--通过临时表
上面都写得很详细,来晚了点帮Ding!