一般功能:
NVL(column_name,m)-如果column_name值为NULL,则返回m
SELECT NVL(empno,0) FROM emp -- returns 0 if empno is NULL
NVL2(column_name,c1,c2)-如果column_name不为NULL则返回c1,如果column_name为NULL则返回c2
SELECT NVL2(empno,empno,'NO EID') from dual -- returns empno if empno IS NOT NULL else returns 'NO EID'
NULLIF(column_name,value)-如果列名=值则返回NULL
SELECT NULLIF(empno,1) FROM emp -- returns NULL IF empno = 1
COALESCE(column1,column2,column3,column4)-如果column1为NULL,则返回column2;如果column2也为NULL,则返回column3,依此类推。
所有列应具有相同的数据类型
SELECT COALESCE(ename,first_name,last_name,'NO NAME') from emp -- returns first_name IF ename IS NULL, IF first_name IS also NULL, then returns last_name, IF last_name IS also NULL, then returns 'NO NAME'
DECODE(column_name,val1,ret_val,val2,ret_val2,ret_ val3)-返回ret_val1 IF column_name = val1 ELSE返回ret_Val2 IF column_name = val2 ELSE返回ret_val3
SELECT DECODE(empno,1,'PM',2,'BM','EMP') FROM emp -- returns 'PM' if empno = 1 ELSE IF empno = 2 then returns 'BM' ELSE returns 'EMP'
CASE(column_name)-CASE可以在PLSQL和SQL语句中使用。
SELECT empno,ename,
(CASE WHEN (empno = 1) THEN 'PM'
WHEN (empno = 2) THEN 'BM'
ELSE 'EMP' END) Position
FROM emp
From: https://bytes.com/topic/oracle/insights/738929-general-functions