2013-07-11 149 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