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

lgz_asp_net lgz_asp_net 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

我数据库表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' 会提示等号错误
大哥们帮帮忙,谢谢了
...全文
评论 6
收藏 已收藏
分享
返回顶部
lgz_asp_net
MS-SQL Server 34,876 成员 254,635 内容