SQL select语句里的if()方法使用

lgz_asp_net 2009-11-17 01:45:13

我数据库表contents里有color (varchar)和publishdate(datetime)两个字段
现在需要查询并显示两个星期之内的color字段设为红色(color='red')根据publishdate字段与当前日期比较(publishdate>getdate()-14)
而两个星期之前的color原本输出显示,不改变表里的数据,只根据条件改变显示的数据
我的语句是这样的
select (case when publishdate>getdate()-111 then color when publishdate<getdate()-111 then color end) as color,
publishdate from contents
如果case when then 里边用color='red' 会提示等号错误
大哥们帮帮忙,谢谢了
...全文
9604 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgz_asp_net 2009-11-17
  • 打赏
  • 举报
回复
谢谢各位的解答,问题已经解决了,谢谢
--小F-- 2009-11-17
  • 打赏
  • 举报
回复
select
(case when datediff(day,publishdate,getdate())<14 then 'red' else color end )as color,
publishdate
from
contents
好汉坡 2009-11-17
  • 打赏
  • 举报
回复
select (case when datediff(day,publishdate,getdate())<14 then 'red' else color end )as color, 
publishdate from contents
sgtzzc 2009-11-17
  • 打赏
  • 举报
回复
select 
(case
when datediff(dd,publishdate,getdate())<=14 then 'red'
else color
end) as color,
publishdate
from
contents
快乐_石头 2009-11-17
  • 打赏
  • 举报
回复
when publishdate>getdate()-111 then 'RED'
--小F-- 2009-11-17
  • 打赏
  • 举报
回复
case when then 可以解决的

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧