在case表达式中返回NULL:SQL server 2008

时间:2016-02-11 10:20:49

标签: sql sql-server-2008

我有以下检查结果集的案例陈述,

SELECT
    CASE
    WHEN EXISTS(
           select CE_Issuedate from t_Certificate
           where CE_CertID_pk = (select IT_certID_fk_ind from t_item
                                 where IT_rfileID_fk = '1482'
                                 group by IT_rfileID_fk) 
    ) 
    THEN  (select CE_Issuedate from t_Certificate
           where CE_CertID_pk = (select IT_certID_fk_ind from t_item
                                 where IT_rfileID_fk = '1482'
                                 group by IT_rfileID_fk)) 
    ELSE  NULL
    END AS 'CE_Issuedate';

我想在此select语句中没有结果时返回NULL。我试过这样但我不确定这是否正确。这个查询正在运行,但我认为没有结果时返回字符串值。

以下脚本返回如下:

enter image description here

但我认为实际上它想要像这样,

enter image description here

请帮帮我。这是对的吗?

1 个答案:

答案 0 :(得分:0)

select max(CE_Issuedate)
from t_Certificate
where EXISTS (select * from t_item
              where IT_rfileID_fk = '1482'
                and CE_CertID_pk = IT_rfileID_fk)

如果未找到行,则MAX返回NULL。否则返回CE_Issuedate。