【问题标题】:SQL CASE Statement (keeping the CASE statement within the WHERE clause)SQL CASE 语句(将 CASE 语句保留在 WHERE 子句中)
【发布时间】:2010-12-13 10:04:29
【问题描述】:
我有一个表名为:
Ext_Meeting_Status
这有字段
- Ext_Meeting_Status_ID
- 文本
值是:
EXT_Meeting_Status_ID Text
1 Draft
2 Published
3 Cancelled
4 Closed
如果日期是今天,我如何返回“已发布”的“文本”字段,否则返回“关闭”。
我尝试使用:
select * from Ext_Meeting_Status
where
GETDATE() = CASE
WHEN ( GETDATE() = '2010-12-13 10:02:31.560' )
THEN ( Ext_Meeting_Status_ID=2)
ELSE ( Ext_Meeting_Status_ID=4)
END
【问题讨论】:
标签:
sql-server
tsql
case
where-clause
sql
【解决方案1】:
select * from Ext_Meeting_Status
where Ext_Meeting_Status_ID =
CASE WHEN (GETDATE() = '2010-12-13 10:02:31.560')
THEN (2)
ELSE (4)
END
我相信这应该可行..
另一个注意事项:将当前日期与精确的毫秒级别进行比较可能无法正常工作,因为当时可能无法执行查询...
你可以试试这样的。
Select getdate(), * from #Temp
Where ID = Case when getdate() between '2010-12-13 05:21:08.240' and '2010-12-13 05:22:08.240'
Then 1
Else 2
End
【解决方案2】:
我认为这是不可能的。您可能想尝试将其全部放入子选择中并在那里使用 CASE。