在CASE声明的THEN部分中的OR运算符

时间:2013-10-20 14:37:42

标签: sql sql-server-2008 operators case or-operator

我有以下SQL CASE语句。我想在“SENNo”条件中包含NULL值:

SEN = CASE --SEN
    WHEN @Subgroup = 'SENA' THEN 'A'
    WHEN @Subgroup = 'SENP' THEN 'P'
    WHEN @Subgroup = 'SENS' THEN 'S'
    WHEN @Subgroup = 'SENNo' THEN 'N'
END

所以我希望它能有效地做到以下几点:

SEN = CASE --SEN
    WHEN @Subgroup = 'SENA' THEN 'A'
    WHEN @Subgroup = 'SENP' THEN 'P'
    WHEN @Subgroup = 'SENS' THEN 'S'
    WHEN @Subgroup = 'SENNo' THEN 'N' OR NULL
END

这不起作用,但我不确定如何构建我的逻辑。

编辑:这是jparask的建议让我走上正轨后的解决方案:

           ISNULL(SEN, 'N') = CASE --SEN
                   WHEN @Subgroup2 = 'SENA' THEN 'A'
                   WHEN @Subgroup2 = 'SENP' THEN 'P'
                   WHEN @Subgroup2 = 'SENS' THEN 'S'
                   WHEN @Subgroup2 = 'SENNo' THEN 'N'
               END

1 个答案:

答案 0 :(得分:2)

SEN = CASE --SEN
    WHEN @Subgroup = 'SENA' THEN 'A'
    WHEN @Subgroup = 'SENP' THEN 'P'
    WHEN @Subgroup = 'SENS' THEN 'S'
    WHEN ISNULL(@Subgroup,'SENNo') = 'SENNo' THEN 'N' 
END

SEN = CASE --SEN
    WHEN @Subgroup = 'SENA' THEN 'A'
    WHEN @Subgroup = 'SENP' THEN 'P'
    WHEN @Subgroup = 'SENS' THEN 'S'
    WHEN @Subgroup IS NULL OR @Subgroup = 'SENNo' THEN 'N' 
END