ORACLE/PLSQL: DECODE FUNCTION
The Oracle/PLSQL DECODE function has the functionality of an IF-THEN-ELSE statement.
The syntax for the Oracle/PLSQL DECODE function is:
DECODE( expression , search , result [, search , result]... [, default] )
expression is the value to compare.
search is the value that is compared against expression.
result is the value returned, if expression is equal to search.
default is optional. If no matches are found, the DECODE function will return default. If default is omitted, then the DECODE function will return null (if no matches are found).
Lets apply this function on emp table.
Emp table has 3 dept numbers like 10,20 and 30.
So if I want to display the different dept names based on ID, I have to use IF THEN ELSE condition.
This entire IF block can be achived using single DECODE().
Query used in Video:
select empno,ename,deptno,DECODE(deptno,10,'DEPT1',20,'DEPT2',30,'DEPT3') from emp;