2013-07-11 152 views
1

我的數據庫表中有一個日期列。帶有日期值的sql case語句

SELECT sDestructionDate FROM tblDocumentHeader 
下面

是上面查詢的結果

enter image description here

我需要爲日期值「1991-01-01」

我期待有一個結果集類似打印空下面。

enter image description here

我想下面的查詢,但它仍然打印「1991-01-01」的日期。

SELECT CASE CONVERT(date,h.sDestructionDate,103) 
WHEN '01/01/1991' 
THEN '' 
ELSE CONVERT(date,h.sDestructionDate,103) END 
AS 'sDestructionDate' 
FROM tblDocumentHeader h 

我不想將結果轉換爲varchar值。下面的查詢確實給了我結果,但我需要從日期格式返回它。

SELECT CASE CONVERT(varchar,h.sDestructionDate,103) 
WHEN '01/01/1991' 
THEN '' 
ELSE convert(varchar,h.sDestructionDate,103) END 
AS 'sDestructionDate' 
FROM tblDocumentHeader h 
+0

會'null'嗎? – Bohemian

+0

@Bohemian我需要嘗試一下,但仍然需要使用case語句嗎? – chamara

+0

sDestructionDate是一個真正的日期列還是varchar? – Blorgbeard

回答

5

我會先將其轉換爲空:

select 
    case 
     when h.sDestructionDate = '01/01/1991' then null 
     else h.sDestructionDate 
    end as sDestructionDate 
from tblDocumentHeader h 

然後看向你報告工具目前一個空白,如果該值null


與混合數據演示文稿是一個錯誤。這種方法很簡單,並保持數據的兩個關注點呈現分開。

+0

thanks.it工作! – chamara

+0

如果sDestructionDate是一個日期字段,查詢將是:'WHEN CAST('1991-01-01'AS DATE)THEN null' – AWippler