你给出的结果没有规律,有取出字母开始的,有的(如最后的4A...)则用数字打头的,不知道你究竟是要什么样的. 用下在的语句可以取出'-'号和'('之前的数据,如何再进一步处理请指示: create table tb(col nvarchar(30)) insert into tb select 'GYTA-2B1(1+5-1.8)' insert into tb select 'GYTA-164B1.3(1+6+12-2.2)' insert into tb select 'GYTA-168B1+48B4(1+6+12-2.2)' insert into tb select 'GYTA-4A1a(1+5-1.8)' go select substring(col,charindex('-',col)+1,charindex('(',col)-charindex('-',col)-1) as col from tb /* col ------------------------------ 2B1 164B1.3 168B1+48B4 4A1a(4 行受影响)*/ go drop table tb
我猜楼主的意思是如果包含B就取B所在的B1,B1.3 如果不包含 就取全部 但是这样不好写
create table t5 (x varchar(30))insert into t5 select 'GYTA-2B1(1+5-1.8)' union all select 'GYTA-164B1.3(1+6+12-2.2)' union all select 'GYTA-168B1+48B4(1+6+12-2.2)' union all select 'GYTA-4A1a(1+5-1.8)'select * from t5x ------------------------------ GYTA-2B1(1+5-1.8) GYTA-164B1.3(1+6+12-2.2) GYTA-168B1+48B4(1+6+12-2.2) GYTA-4A1a(1+5-1.8)select case when charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))>0 then case when charindex('+',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1, charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''))>0 then stuff( stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1, charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''), patindex('%+%',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1, charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''))+1, charindex('B',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1, charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''),2)- patindex('%+%',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1, charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''))-1 , '' ) else stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1, charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,'') end else substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5) end x from t5x --------- B1 B1.3 B1+B4 4A1a
但是不好做关键是这行数据GYTA-168B1+48B4(1+6+12-2.2)
用下在的语句可以取出'-'号和'('之前的数据,如何再进一步处理请指示:
create table tb(col nvarchar(30))
insert into tb select 'GYTA-2B1(1+5-1.8)'
insert into tb select 'GYTA-164B1.3(1+6+12-2.2)'
insert into tb select 'GYTA-168B1+48B4(1+6+12-2.2)'
insert into tb select 'GYTA-4A1a(1+5-1.8)'
go
select substring(col,charindex('-',col)+1,charindex('(',col)-charindex('-',col)-1) as col from tb
/*
col
------------------------------
2B1
164B1.3
168B1+48B4
4A1a(4 行受影响)*/
go
drop table tb
如果不包含 就取全部
但是这样不好写
create table t5 (x varchar(30))insert into t5
select 'GYTA-2B1(1+5-1.8)' union all
select 'GYTA-164B1.3(1+6+12-2.2)' union all
select 'GYTA-168B1+48B4(1+6+12-2.2)' union all
select 'GYTA-4A1a(1+5-1.8)'select * from t5x
------------------------------
GYTA-2B1(1+5-1.8)
GYTA-164B1.3(1+6+12-2.2)
GYTA-168B1+48B4(1+6+12-2.2)
GYTA-4A1a(1+5-1.8)select case when charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))>0 then
case when charindex('+',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1,
charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''))>0 then
stuff(
stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1,
charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''),
patindex('%+%',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1,
charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''))+1,
charindex('B',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1,
charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''),2)-
patindex('%+%',stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1,
charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,''))-1
,
''
)
else
stuff(substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5),1,
charindex('B',substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5))-1,'') end
else substring(x,charindex('GYTA-',x)+5,charindex('(',x)-charindex('GYTA-',x)-5) end x
from t5x
---------
B1
B1.3
B1+B4
4A1a