NVL: Converts a null value to an actual value
NVL2:If expr1 is not null, NVL2 returns expr2. If expr1 is null, NVL2 returns expr3. The argument expr1 can have any data type.
NULLIF:Compares two expressions and returns null if they are equal; returns the first expression if they are not equal
COALESCE:Returns the first non-null expression in the expression list.
nvl(COMMISSION_PCT,0)
如果第一个参数为null,则返回第二个参数
如果第一个参数为非null,则返回第一个参数
SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- ------------------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20 7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10 1111 rusky clerk 7698 111 20 已选择15行。 SQL> select nvl(comm,0) from emp; NVL(COMM,0) ----------- 0 300 500 0 1400 0 0 0 0 0 0 0 0 0 111 已选择15行。 SQL>
NVL2(COMMISSION_PCT,‘SAL_COMM‘,‘SAL‘)
如果第一个参数为null,则返回第三个参数
如果第一个参数为非null,则返回第二个参数
(例:如果奖金为空,则返回工资,如果奖金非空,则返回奖金与工资总和)
SQL> select nvl2(comm,comm+nvl(sal,0),nvl(sal,0)) from emp; NVL2(COMM,COMM+NVL(SAL,0),NVL(SAL,0)) ------------------------------------- 800 1900 1750 2975 2650 2850 2450 3000 5000 1500 1100 950 3000 1300 111 已选择15行。
nullif(expr1,expr2)
如果两个参数值相等,则返回 null
如果两个参数不等,则返回第一个表达式值
SQL> select nullif(to_char(hiredate,‘YYYY‘),‘1981‘) from emp; NULL ---- 1980 1987 1987 1982 已选择15行。 SQL>
COALESCE(EXPR1,EXPR2,EXPR3...EXPRn)
从左往右数,遇到第一个非null值,则返回该非null值。多层判断
SQL> select coalesce(sal+comm,sal+nvl(comm,0),nvl(sal,0)+nvl(comm,0)) from emp; COALESCE(SAL+COMM,SAL+NVL(COMM,0),NVL(SAL,0)+NVL(COMM,0)) --------------------------------------------------------- 800 1900 1750 2975 2650 2850 2450 3000 5000 1500 1100 950 3000 1300 111 已选择15行。 SQL>
时间: 2024-11-03 01:17:04