开发者

SQL CASE Statement (keeping the CASE statement within the WHERE clause)

开发者 https://www.devze.com 2023-01-30 11:49 出处:网络
I have a table named: Ext_Meeting_Status This has fields Ext_Meeting_Status_ID TEXT The values are: EXT_Meeting_Status_IDText

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 ( E开发者_如何学Cxt_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号