问:
从数据表中选择一个字段“field”,如果“field”值是1或NULL就赋值为1,其它情况为0,该怎么写啊?这样写对不对啊?
(CASE field
WHEN '1' THEN '1'
WHEN NULL THEN '1'
ELSE '0' END
) AS field
满意回答:
不要使用when null来判断,等于NULL的时候判断不出来的
你可以当字段等于NULL时给一个默认值。比如
(CASE isnull(field,'')
WHEN '1' THEN '1'
WHEN '' THEN '1'
ELSE '0' END
) AS field
或者在when后边写条件
case when field = '1' then '1' when field is null then '1' else '0' end as field
你可以当字段等于NULL时给一个默认值。比如
(CASE isnull(field,'')
WHEN '1' THEN '1'
WHEN '' THEN '1'
ELSE '0' END
) AS field
或者在when后边写条件
case when field = '1' then '1' when field is null then '1' else '0' end as field