表结构入下:
  序号   提示1    提示2
  1       优       null
  2       良       差
  3       null     中要求拼凑如下字符串   如第二行‘提示1:良 提示2:差
对于null或空字符串值,不出现提示1或提示2 如第一行应该是 ‘提示1:优’

解决方案 »

  1.   

    select 
        序号, 
         case when 提示1 is not null then '提示1:'+提示1 else '' end
        +case when 提示2 is not null then '提示2:'+提示2 else '' end
    from 表
      

  2.   

    create table tbl(xh int identity(1,1),fld1 nvarchar(10),fld2 nvarchar(10))
    insert into tbl select N'优',null
          union all select N'良',N'差'
          union all select Null,N'中'select (N'提示1:'+fld1),(N'提示2:'+fld2) from tbl
    drop table tbl
    ------------------
    提示1:优 NULL
    提示1:良 提示2:差
    NULL 提示2:中
      

  3.   

    select case when 提示1 is null then '' else '提示1:' + 提示1 end  + case when 提示2 is null then '' else '提示2:' + 提示2 end
      

  4.   

    --这样吗?--测试数据
    create table tb(序号 int,提示1 varchar(10),提示2 varchar(10))
    insert tb select 1,'优',null
    insert tb select 2,'良','差'
    insert tb select 3,null,'中'
    --查询语句
    select 序号,提示=case when isnull(提示1,'')='' then '' else '提示1:'+提示1 end +''+case when isnull(提示2,'')='' then '' else '提示2:'+提示2 end
    from tb
    --结果
    /*
    序号     提示
    --------------
    1 提示1:优
    2 提示1:良提示2:差
    3 提示2:中
    */