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

发布于 2024-10-07 07:46:21 字数 521 浏览 2 评论 0原文

我有一个名为:

Ext_Meeting_Status

它具有字段

  1. Ext_Meeting_Status_ID
  2. TEXT

值是:

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

I have a table named:

Ext_Meeting_Status

This has fields

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-10-14 07:46:21
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
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
差↓一点笑了 2024-10-14 07:46:21

我认为这是不可能的。您可能想尝试将其全部放入子选择中并在那里使用 CASE。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文