请问一下表A:
ID    comment 
1        12*13
2        124*3
3        ASD*4R
4        23D
.....规则,若有*,截取*前面的数值,若没有,只需带出原有的数值

解决方案 »

  1.   


    select id,(case when charindex('*',comment)>0 then left(comment,charindex('*',comment)-1) else comment) comment
    from a
      

  2.   


    select id,(case when charindex('*',comment)>0 
                    then left(comment,charindex('*',comment)-1) 
               else comment end/*上边少个end*/) comment
    from a
      

  3.   

    select ID , substring(comment ,1,charindex('*',comment ) from A
      

  4.   

    select id,left(comment,len(comment)-charindex('*',comment)) from tb
      

  5.   


    declare @T table (ID int,comment varchar(6))
    insert into @T
    select 1,'12*13' union all
    select 2,'124*3' union all
    select 3,'ASD*4R' union all
    select 4,'23D'select comment=left(comment,case when charindex('*',comment)-1=-1 
    then len(comment) else  charindex('*',comment)-1 end ) from @T
    /*
    comment
    -------
    12
    124
    ASD
    23D
    */
      

  6.   


    declare @T table (ID int,comment varchar(6))
    insert into @T
    select 1,'12*13' union all
    select 2,'124*3' union all
    select 3,'ASD*4R' union all
    select 4,'23D'select left(comment+'*',charindex('*',comment+'*')-1) from @T
    /*
    12
    124
    ASD
    23D
    */
      

  7.   

    declare @T table (ID int,comment varchar(6))
    insert into @T
    select 1,'12*13' union all
    select 2,'124*3' union all
    select 3,'ASD*4R' union all
    select 4,'23D'select id,left(comment,len(comment)-charindex('*',reverse(comment))) from @T/*
    id          
    ----------- ------
    1           12
    2           124
    3           ASD
    4           23D(4 行受影响)
    */