简体   繁体   English

SQL CASE语句(将CASE语句保留在WHERE子句中)

[英]SQL CASE Statement (keeping the CASE statement within the WHERE clause)

I have a table named: 我有一个名为的表:

Ext_Meeting_Status

This has fields 这有领域

  1. Ext_Meeting_Status_ID Ext_Meeting_Status_ID
  2. TEXT 文本

The values are: 价值观是:

EXT_Meeting_Status_ID   Text
1   Draft
2   Published
3   Cancelled
4   Closed

How do i return the "Text" field of "Published" if date is today, else return "Close". 如果日期是今天,我如何返回“已发布”的“文本”字段,否则返回“关闭”。

I tried using: 我试过用:

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
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 

I believe this should work.. 我相信这应该有效..

One more note : Comparing current date to the exact millisecond level might not work as the query may not get executed at that time... 还有一点需要注意:将当前日期与精确的毫秒级别进行比较可能不起作用,因为此时查询可能无法执行...

You may try something like this. 你可以尝试这样的事情。

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

I don't think it's possible. 我不认为这是可能的。 You may want to try to put it all into a sub-select and use the CASE there. 您可能希望尝试将所有内容放入子选择中,然后在其中使用CASE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM