decode versus case

8
Decode V/S Case Snehanshu CASE AND DECODE : Two powerful constructs of SQL

Upload: snehanshu19

Post on 12-Oct-2015

39 views

Category:

Documents


0 download

DESCRIPTION

PPT to describe decode versus case in oracle

TRANSCRIPT

  • Decode V/S CaseSnehanshu

    CASE AND DECODE : Two powerful constructs of SQL

  • HistoryIF-THEN-ELSE FunctionalityCASE was introduced in Oracle 8.1.6 as a replacement for the DECODE . Anyway it is much better option than DECODE as it is ,1. More Flexible than DECODE2. More easier to read3. ANSI Compatible4. compatible in PL/SQL Context

  • CaseExpression SyntaxCASE [ expression ] WHEN Value_1 THEN result_1 WHEN Value_2 THEN result_2 ... WHEN Value_n THEN result_n [ELSE else_result]ENDExample 1:-Double salary of SH_CLERCK and Triple salary of AC_MGR and keep rest salary same.

  • Decode1. DECODE( expression , search , result [, search , result]... [, default] )

    Example 1:-Double salary of SH_CLERCK and Triple salary of AC_MGR and keep rest salary same.

  • CaseCondition SyntaxCASE WHEN Condition_1 THEN result_1 WHEN Condition_2 THEN result_2 ... WHEN Condition_n THEN result_n [ELSE else_result]ENDExample 1:-Double salary of SH_CLERCK and Triple salary of AC_MGR and keep rest salary same.

  • Example 2-- Decode can not do thisDisplay the hiked (*2)the salary for only for those employees who were hired before 01-JAN-1982.

  • Example 3:- What about NULLSELECT DECODE (NULL,NULL,'NULL','NOTNULL') NICE FROM DUAL;SELECT CASE NULL WHEN NULL THEN 'NULL' ELSE 'NOTNULL' END NICE FROM DUAL;-- Is NULL

  • SELECT EMPNO , JOB , DECODE( JOB,'MANAGER',SAL,'NOSAL' ) SAL FROM EMP;SELECT EMPNO , JOB , CASE JOB WHEN 'MANAGER' THEN SAL ELSE 'NOSAL' END FROM EMP;