有一临时表   
declare  @t  table([id]  int,[name]  varchar(100))  
insert  into  @t  32
select  1,                        '#注意    #正常    #异常'      
union  all  select  2,                        '#正常    #注意'        
union  all  select  3,                        '#异常    #正常    #正常  #正常'    
 
select * from @t
我要得到查询结果是name字段下面#后面级别最高的信息
id name
1 异常
2 注意
3 异常

解决方案 »

  1.   

    declare  @t  table([id]  int,[name]  varchar(100))  
    insert  into  @t
    select  1,                        '#注意    #正常    #异常'      
    union  all  select  2,                        '#正常    #注意'        
    union  all  select  3,                        '#异常    #正常    #正常  #正常'    select
    id,
    case
    when charindex('#异常',name)>0 then '#异常'
    when charindex('#注意',name)>0 then '#注意'
    when charindex('#正常',name)>0 then '#正常'
    end as name
    from @t/*id          name  
    ----------- ----- 
    1           #异常
    2           #注意
    3           #异常
    */