0% found this document useful (0 votes)
64 views

Decode and Case

The document compares the DECODE and CASE functions in Oracle. It states that: 1) CASE was introduced in Oracle 8.1.6 to provide a more standard and powerful alternative to DECODE. 2) While DECODE can only perform simple mappings, CASE can handle more complex conditional logic including ranges and subqueries. 3) CASE can be used more flexibly than DECODE both in SQL and PL/SQL.

Uploaded by

Murali Mohan G
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Decode and Case

The document compares the DECODE and CASE functions in Oracle. It states that: 1) CASE was introduced in Oracle 8.1.6 to provide a more standard and powerful alternative to DECODE. 2) While DECODE can only perform simple mappings, CASE can handle more complex conditional logic including ranges and subqueries. 3) CASE can be used more flexibly than DECODE both in SQL and PL/SQL.

Uploaded by

Murali Mohan G
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Decode and Case Explanation

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

Both can be used in sql as well as pl/sql

i. CASE complies with ANSI SQL. DECODE is proprietary to Oracle.


ii. CASE executes faster than DECODE.
iii. Difference between the DECODE and CASE is that DECODE cannot be used
handle Ranged values, whereas CASE is capable of that. CASE is able to

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.

vii. CASE can even work even as a parameter of a function/procedure, while


DECODE cannot.

SQL> var a varchar2(5);


SQL> exec :a := 'THREE';
  
PL/SQL procedure successfully completed.
  
SQL>
SQL> create or replace procedure proc_test (i number)
  2  as
  3  begin
  4    dbms_output.put_line('output = '||i);
  5  end;
  6  /
  
Procedure created.
  
SQL> exec proc_test(decode(:a,'THREE',3,0));
BEGIN proc_test(decode(:a,'THREE',3,0)); END;
  
                *
ERROR at line 1:
ORA-06550: line 1, column 17:
PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL
statement only
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
  
SQL> exec proc_test(case :a when 'THREE' then 3 else 0 end);
output = 3
  
PL/SQL procedure successfully completed.

viii. CASE expects datatype consistency, DECODE does not


Compare the two examples – DECODE gives you a result, CASE gives a datatype
mismatch error.
SQL> select decode(2,1,1,
  2                 '2','2',
  3                 '3') t
4 from dual; 

         T
----------
         2

SQL> select case 2 when 1 then '1'


  2              when '2' then '2'
  3              else '3'
  4         end
  5  from dual;
            when '2' then '2'
                 *
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected NUMBER got CHAR

You might also like