select right(字段名,case when charindex(']',reverse(字段名))-1>=0 then charindex(']',reverse(字段名))-1 else 0 end)from 表名

解决方案 »

  1.   

    --例子:declare @s varchar(100)set @s='[7]abc'select right(@s,case when charindex(']',reverse(@s))-1>=0 then charindex(']',reverse(@s))-1 else 0 end)--返回:abcset @s='fffabc'select right(@s,case when charindex(']',reverse(@s))-1>=0 then charindex(']',reverse(@s))-1 else 0 end)--返回空串
      

  2.   

    create table tb(id int,col_1 varchar(20))
    insert into tb values(1,'[1]aaaaaa')
    insert into tb values(2,'[7]bbbbbbbbbbb')
    insert into tb values(3,'[13]cccccccc')
    insert into tb values(4,'[0]ddddddd')
    select id,substring(col_1,charindex(']',col_1) + 1 , len(col_1)) col_1 
    from tb
    where charindex(']',col_1) > 0
    drop table tb/*
    id          col_1                
    ----------- -------------------- 
    1           aaaaaa
    2           bbbbbbbbbbb
    3           cccccccc
    4           ddddddd
    (所影响的行数为 4 行)
    */
      

  3.   

    --连续取反没有上面的简单.create table tb(id int,col_1 varchar(20))
    insert into tb values(1,'[1]aaaaaa')
    insert into tb values(2,'[7]bbbbbbbbbbb')
    insert into tb values(3,'[13]cccccccc')
    insert into tb values(4,'[0]ddddddd')
    select id,reverse(left(reverse(col_1),charindex(']',reverse(col_1))-1)) col_1 
    from tb
    where charindex(']',col_1) > 0
    drop table tb/*
    id          col_1                
    ----------- -------------------- 
    1           aaaaaa
    2           bbbbbbbbbbb
    3           cccccccc
    4           ddddddd
    (所影响的行数为 4 行)
    */
      

  4.   

    select id, right(col_1 , len(col_1)-charindex(']',col_1) ) as col_1 from tb