我数据库表contents里有color  (varchar)和publishdate(datetime)两个字段 
现在需要查询并显示两个星期之内的color字段设为红色(color='red')根据publishdate字段与当前日期比较(publishdate>getdate()-14) 
而两个星期之前的color原本输出显示,不改变表里的数据,只根据条件改变显示的数据 
我的语句是这样的 
select (case when publishdate>getdate()-14 then color when publishdate <getdate()-14 then color end) as color, 
publishdate from contents 
如果case when then 里边用color='red' 会提示等号错误 
大哥们帮帮忙,谢谢了

解决方案 »

  1.   

    可以用 case when 的方式。
      

  2.   

    1.case 字段 when a(字段的值) then '你要的' when b then  '你要的' end as 字段名
    2.case when (a<b) then  ''
      when (b<c) then '' end as 字段名
      

  3.   


    select 
         (case when publishdate > getdate()-14 then 'Red' 
               else color 
              end) as color
      

  4.   

    select case when (publishdate>getdate()-14) then 'red' when (publishdate <getdate()-14) then 'yellow'end as color, 试试
      

  5.   

    select (case when publishdate>getdate()-14 then 'red' when publishdate <getdate()-14 then color end) as color, 
    publishdate from contents