如表A:色号
1yi2-33e-9823i-888p
uu-45it-77888
99-ty99-ii333
通过SQL语句把表A字段色号第一个'-'前面的字符删掉。得到如下:33e-9823i-888p
45it-77888
ty99-ii333

解决方案 »

  1.   

    select stuff(色号,1,charindex('-',色号),'') as 色号 
    from a
      

  2.   

    if object_id('[tb]') is not null drop table [tb] 
     go 
    create table [tb]([col] varchar(30))
    insert [tb] select '1yi2-33e-9823i-888p'
    union all select 'uu-45it-77888'
    union all select '99-ty99-ii333'update tb 
    set col=stuff(col,1,charindex('-',col),'')select * from tb
    /*
    col
    ------------------------------
    33e-9823i-888p
    45it-77888
    ty99-ii333(3 行受影响)
    */
      

  3.   

    严密点select case when charindex('-',色号) > 0 then stuff(色号,1,charindex('-',色号),'') else 色号 end as 色号 
    from a
      

  4.   


     Select substring(色號,charindex('-',色號)+1, len(色號)-(charindex('-',色號)+1))
      

  5.   

    if object_id('[tb]') is not null drop table [tb] 
     go 
    create table [tb]([col] varchar(30))
    insert [tb] select '1yi2-33e-9823i-888p'
    union all select 'uu-45it-77888'
    union all select '99-ty99-ii333'update tb 
    set col=right(col,len(col)-charindex('-',col))select * from tb
    /*
    col
    ------------------------------
    33e-9823i-888p
    45it-77888
    ty99-ii333(3 行受影响)
    */再一种方法
      

  6.   

    create table [tb]([col] varchar(30))
    insert [tb] select '1yi2-33e-9823i-888p'
    union all select 'uu-45it-77888'
    union all select '99-ty99-ii333' 
    select *,stuff(col,1,charindex('-',col,1),'') as col2  from tb/*
      col                         col2
    -------------------     ----------------
    1yi2-33e-9823i-888p 33e-9823i-888p
    uu-45it-77888         45it-77888
    99-ty99-ii333         ty99-ii333
    */drop table tb
      

  7.   

    select  substring('1yi2-33e-9823i-888p',charindex('-','1yi2-33e-9823i-888p',0)+1,len('1yi2-33e-9823i-888p')) 
      

  8.   

    right(col,len(col)-charindex('-',col))这个很容易理解.也简单