我有以下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
答案 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