我正在嘗試使用status
列和DateSent
列查詢表。在我的查詢中,我想將這兩列合併爲一個sent
列 - 因此如果狀態爲c
(已關閉/已發送),則顯示DateSent
- 但如果不是,則顯示相應的狀態。如果日期爲空(SQL Server CE)
任何不是sent
的行都將有DateSent
作爲NULL。
所需的輸出:
ID Sent
-----------------
1 Queued
2 Failed
3 2013-02-28
4 Queued
我當前的SQL Server CE查詢:
select
ID, DateAdded,
case when DateSent is null then
(case when Status='W' then 'Waiting' else
case when Status='O' then 'Uploaded' else
case when Status='F' then 'Failed' else
case when Status='E' then 'Expired' else
'Unknown' end end end end) else DateSent
end as Sent
from table
但else DateSent
部分上線7拋出一個錯誤:
There was a syntax error in the date format. [0, 0, 0,Expression: Failed,,]
可能有人幫助我如何解決這個問題?
編輯:問題的一種更容易閱讀的片段是:
select case when DateSent is null
then 'null' else DateSent --<---- problem is here
end as Sent from table
是的,我的不好。我誤讀了。但你爲什麼這麼複雜?你可以用一個CASE表達式來做同樣的事情。 – 2013-02-28 16:34:09
@ypercube我在一個月前開始學習SQL,所以我不太熟悉最簡單/最有效的方法:) – 2013-02-28 16:36:41