企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### SQL中的ISNULL函数介绍 * * * * * ISNULL 使用指定的替换值替换 NULL。 #### 语法 ISNULL ( check_expression , replacement_value ) #### 参数 check_expression 将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。 replacement_value 在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。 返回类型: 返回与 check_expression 相同的类型。 #### 注释 如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。 ascore 表内容如下: ![](https://box.kancloud.cn/d837f66c21a60d8c2fa9fb27b88b3a3f_506x173.png) 查询出其中成绩(score)小于等于60的学生信息保存至表变量@tempTable中,当学生成绩为空时,成绩记为0。 ~~~ declare @tempTable table( stuname nchar(10), stuage int, stuscore float); insert into @tempTable select name,age,ISNULL(score,0) from ascore where ISNULL(score,0)<=60 select * from @tempTable ~~~ 运行结果: ![](https://box.kancloud.cn/b6211f5532ff0a1f3cdf57ef81ae2e41_253x94.png) #### ISNULL 与 AVG 一起使用 下面的示例查找学生的平均成绩,用值 70.00 替换 titles 表的 score 列中的所有 NULL 条目。 ~~~ SELECT AVG(ISNULL(score, 70.00)) FROM ascore ~~~ * * * * * MySQL语句参考: http://www.w3school.com.cn/sql/sql_isnull.asp