我的案例陈述有什么问题?
如果值是数字,那么我想检查它是否大于或等于5,如果为真则返回1,否则返回0.有没有优雅的方法这样做?
CASE下面嵌套在另一个CASE
中CASE
WHEN(
xyz <> a
AND abc <> 3
AND
CASE
WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1 THEN
CASE
WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5 THEN 1
ELSE 0
END
ELSE 0
END
)
THEN 1
ELSE 0
在WHEN ISNUMERIC()
之前的第二个AND之后,我在第一个CASE上得到以下错误An expression of non-boolean type specified in a context where a condition is expected.
答案 0 :(得分:5)
最外层案例陈述中缺少标准:
CASE
WHEN(
xyz <> a
AND abc <> 3
AND
CASE
WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1 THEN
CASE
WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5 THEN 1
ELSE 0
END
ELSE 0
END = ? -- Missing criteria
)
THEN 1
ELSE 0
修改:不确定是否有理想的格式化方式,但我发现以下内容更容易理解:
CASE WHEN ( xyz <> a
AND abc <> 3
AND CASE WHEN ISNUMERIC(LEFT(o.RepCode, 1)) = 1
THEN CASE WHEN CONVERT(INT, LEFT(o.RepCode, 1)) >= 5
THEN 1
ELSE 0
END
ELSE 0
END = ? -- Missing Criteria
)
THEN 1
ELSE 0
END