ORACLE函数之空值

NVL、NVL2、NULLIF、COALESCE
NVL
SELECT employee_id,salary,commission_pct,salary+salarycommission_pct FROM employees;
SELECT employee_id,salary,commission_pct,salary+salary
NVL(commission_pct,0) FROM employees;
在这里插入图片描述
在这里插入图片描述
nvl(exp1,exp2) exp1为空值输出exp2 非空输出原值
SELECT employee_id,salary,commission_pct,salary+salary*NVL(commission_pct,‘ABC’) FROM employees;注意参与运算时一定要让参数个数相同,不然会报错

NVL2
SELECT employee_id,salary,commission_pct,salary+salary*NVL2(commission_pct,‘sal+comm’,‘sal’) FROM employees;错误

SELECT employee_id,salary,commission_pct,NVL2(commission_pct,‘sal+comm’,‘sal’) FROM employees;
在这里插入图片描述
nvl2(exp1,exp2,exp3)如果exp1为空值输出exp3,若为非空,输出exp2

NULLIF
SELECT length(first_name),length(last_name),nullif(length(first_name),length(last_name)) “Res” FROM employees;
在这里插入图片描述
nullif(exp1,exp2)exp1等于exp2时为空值 否则输出exp1

COALESCE
SELECT first_name,commission_pct,manager_id,COALESCE(commission_pct,manager_id,‘NO commission and no manager’) FROM employees;错误格式不统一
SELECT first_name,commission_pct,manager_id,COALESCE(TO_CHAR(commission_pct),TO_CHAR(manager_id),‘NO commission and no manager’) FROM employees;需要to_char转化一次
在这里插入图片描述
COALESCE(exp1,exp2,…expn)如果exp1为空 则继续看exp2, 直到不为空输出结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值