isnull函数与case when then end
1.在一个表中查询指定字段值为null的记录
最直接想法:select * from 表名 where 字段 = null
测试结果:没有报错,但什么也没有显示出来。
正确答案:select * from 表名 where 字段 is null
2.SQL的ISNULL问题:
使用ISNULL函数,可以使用指定的值替换null
语法如下:ISNULL ( check _ expression , replacement_value )
即:当查询出的check _ expression的值为null时,即使用 replacement_value(比如0之类的)替换掉。
注意:第2个参数是和第1个参数同类型的值类型,不能也是表达式,否则会报错。
例:select id, isnull(user_name, 'noname') from user
3.case when then end语句:
举例:case when values is null then 0 else values end
即:当字段values的值为null时,使用0进行表示,否则直接values字段的数值。