Decode and Case
Decode and Case
DECODE Function and CASE statements in Oracle both provide a conditional construct,
of this form:
if A = n1 then A1
else if A = n2 then A2
else X
Databases before Oracle 8.1.6 had only the DECODE function. CASE was introduced in
Oracle 8.1.6, as a standard, more meaningful and more powerful function.
Everything DECODE can do, CASE can. There is a lot more that you can do with CASE,
though, which DECODE cannot
Decode:
SQL> SELECT ename,
2 DECODE(deptno, 10, 'ACCOUNTING',
3 20, 'RESEARCH',
4 30, 'SALES',
5 40, 'OPERATIONS',
6 'UNKNOWN') AS department
7 FROM emp
8 WHERE rownum < 4
9 /
ENAME DEPARTMENT
---------- ----------
SMITH RESEARCH
ALLEN SALES
WARD SALES
Case:
SQL> SELECT ename,
2 CASE deptno
3 WHEN 10 THEN 'ACCOUNTING'
4 WHEN 20 THEN 'RESEARCH'
5 WHEN 30 THEN 'SALES'
6 WHEN 40 THEN 'OPERATIONS'
7 ELSE 'UNKNOWN'
8 END AS department
9 FROM emp
10 WHERE rownum < 4
11 /
ENAME DEPARTMENT
---------- ----------
SMITH RESEARCH
ALLEN SALES
WARD SALES
handle ranged values by using >, <, BETWEEN. To achieve the same effect with
DECODE, ranges of data had to be forced into discrete form making unwieldy
code.
An example of putting employees in grade brackets based on their salaries – this can be
done elegantly with CASE.
1
2 SQL> select ename
3 2 , case
4 3 when sal < 1000
5 4 then 'Grade I'
6 5 when (sal >=1000 and sal < 2000)
6 then 'Grade II'
7 7 when (sal >= 2000 and sal < 3000)
8 8 then 'Grade III'
9 9 else 'Grade IV'
10 10 end sal_grade
11 from emp
11 12 where rownum < 4;
12
13ENAME SAL_GRADE
14---------- ---------
15SMITH Grade I
16ALLEN Grade II
WARD Grade II
17
18
iv. DECODE works with expressions which are scalar values, whereas CASE can
also work with predicates and subqueries in searchable form.
SQL> select ename,
2 case
3 when ename in ('KING','SMITH','ALLEN') then
4 'Managers'
5 when exists (select 1 from dept where deptno = emp.deptno and deptno = 10) then
6 'Guy from 10th'
7 else
8 'Another person'
9 end blah_blah
10 from emp
11 / where rownum < 5;
ENAME BLAH_BLAH
---------- --------------
SMITH Managers
WARD Another person
CLARK Guy from 10th
SCOTT Another person
4 rows selected.
v. Decode automatically converts the second return value to the datatype of the
first return value. And if the first return value is null, then the second return
value is converted to varchar2. BE VERY CAREFUL USING DECODE FUNCTION
IF THE FIRST RETURN VALUE IS NULL i.e.
max(decode(status,'BC',NULL,create_date))
In this case, the create_date column will be converted to varchar2 type, and
so the max may give errors. CASE-WHEN is obviously a better choice in this
regard.
vi. DECODE can work as a function inside SQL only. CASE can be a more efficient
substitute for IF-THEN-ELSE in PL/SQL.
SQL> declare
2 grade char(1);
3 begin
4 grade := 'b';
5 case grade
6 when 'a' then dbms_output.put_line('excellent');
7 when 'b' then dbms_output.put_line('very good');
8 when 'c' then dbms_output.put_line('good');
9 when 'd' then dbms_output.put_line('fair');
10 when 'f' then dbms_output.put_line('poor');
11 else dbms_output.put_line('no such grade');
12 end case;
13 end;
14 /
PL/SQL procedure successfully completed.
T
----------
2