数据表中有如下数据,如: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),取出其中的字符串如B1,B1.3,B1+B4,4A1a。如何用语句实现,谢谢。

解决方案 »

  1.   

    http://hi.baidu.com/golotus/blog/item/a06782cadbd7a140f21fe7dc.html看看这个
      

  2.   

    你这个有规律
    但是不好做关键是这行数据GYTA-168B1+48B4(1+6+12-2.2)
      

  3.   

    你给出的结果没有规律,有取出字母开始的,有的(如最后的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
      

  4.   

    我猜楼主的意思是如果包含B就取B所在的B1,B1.3
    如果不包含 就取全部
    但是这样不好写
      

  5.   


    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
      

  6.   

    真是人才……select substring(col,charindex('-',col)+1,charindex('(',col)-charindex('-',col)-1)  这样子应该就可以达到楼主的要求了……赞一个……
      

  7.   

    [code=SQL]select * from table [code]
      

  8.   

    能不能实现只取出B1、B1.3,A1a呢,求解?
      

  9.   

         能不能实现只取出B1、B1.3,A1a呢,求解?