DECODE and CASE both provide conditional constructs in 'if...else if...else' form.
Prior to Oracle 8.1.6 DECODE was the only function available for conditional check. In Oracle 8.1.6, CASE was introduced which was more efficient, more powerful, more structured than DECODE.
Everything DECODE can do CASE can do, but CASE can do a lot more which DECODE cannot.
Following are the high level differences between CASE and DECODE.
Everything DECODE can do CASE can do, but CASE can do a lot more which DECODE cannot.
Following are the high level differences between CASE and DECODE.
- CASE can work with logical operators other than '=' : Decode performs only equality checks whereas is capable of working with operators like '<', '>', '<=', '>='.
SQL>selectename2 ,case3whensal < 10004then'Grade I'5when(sal >=1000andsal < 2000)6then'Grade II'7when(sal >= 2000andsal < 3000)8then'Grade III'9else'Grade IV'10endsal_grade11fromemp12whererownum < 4; - CASE can work with predicates and searchable subqueries : For example 'IN' statement or simple subqueries as shown below
SQL>selecte.ename,2case3-- predicate with "in"4-- mark the category based on ename list5whene.enamein('KING','SMITH','WARD')6then'Top Bosses'7-- searchable subquery8-- identify if this emp has a reportee9whenexists (select1fromemp emp110whereemp1.mgr = e.empno)11then'Managers'12else13'General Employees'14endemp_category15fromemp e16whererownum < 5; - CASE can work as a PL-SQL construct : Decode can be used as a function inside PL-SQL query whereas CASE can be used as a PL-SQL block or code construct as follows as a great substitute of IF-THEN-ELSE
SQL>declare2 gradechar(1);3begin4 grade :='b';5casegrade6when'a'thendbms_output.put_line('excellent');7when'b'thendbms_output.put_line('very good');8when'c'thendbms_output.put_line('good');9when'd'thendbms_output.put_line('fair');10when'f'thendbms_output.put_line('poor');11elsedbms_output.put_line('no such grade');12endcase;13end;14 /CASE can even work as a parameter to the procedures whereas DECODE cannotSQL>execproc_test(case:awhen'THREE'then3else0end); - CASE handles NULL differently :
Check out the different results with DECODE vs NULL.
123456789SQL>selectdecode(null2 ,null,'NULL'3 ,'NOT NULL'4 ) null_test5fromdual;NULL_TEST----NULLThe searched CASE works as does DECODE.12345678910SQL>selectcasenull2whennull3then'NULL'4else'NOT NULL'5endnull_test6fromdual;NULL_TEST--------NOTNULL
1234567891011SQL>selectcase2whennullisnull3then'NULL'4else'NOT NULL'5endnull_test6*fromdualSQL> /NULL_TEST--------NULL - CASE expects data type consistency : CASE expects data type consistency in expression / parameters provided to the function whereas DECODE ignores the parameter data types.
- CASE is ANSI SQL compliant :
CASE complies with ANSI SQL. DECODE is proprietary to Oracle. - READABILITY : In simple cases DECODE is short and sweet to read and understand. However in complex cases it is always preferred to go with CASE to avoid bug prone code.
No comments:
Post a Comment