带有日期值的sql case语句

时间:2013-07-11 07:10:23

标签: sql sql-server

我的数据库表中有一个日期列。

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

1 个答案:

答案 0 :(得分:7)

我首先将其转换为null:

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

如果值为null

,请查看您的报告工具显示空白

将数据与演示文稿混合是一个错误。这种方法很简单,可以将数据呈现的两个问题分开。

相关问题