Translate

DECODE Vs. CASE


What is the Difference Between DECODE and CASE? Which one is useful and when?
DECODE 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

Everything DECODE can do, CASE can. There is a lot else CASE can do though, which DECODE cannot. We’ll go through detailed examples in this blog.
Some differences between CASE and DECODE:
  •  CASE is a statement while DECODE is a function.
  •  CASE can work with logical operators other than ‘=’ :   
  •  DECODE performs an equality check only. CASE is capable of other logical comparisons such as < ,> ,BETWEEN , LIKE etc. 

  • CASE can work with predicates and searchable sub queries.   
  • DECODE works with expressions that are scalar values only. CASE can work with predicates and sub queries in searchable form.

  • CASE can work as a PL/SQL construct but DECODE is used only in SQL statement.
  • DECODE can work as a function inside SQL only. CASE can be an efficient substitute for IF-THEN- ELSE in PL/SQL. In below example, if you replace CASE with DECODE then it gives error.

  • CASE can be used as parameter of a function/procedure. CASE can even work as a parameter to a procedure call, while DECODE cannot.
                      Exec MyProc(case :A when 'THREE' then 3 else 0 end);
                      Above statement will not give error but below statement gives error.
                      Exec MyProc(decode(:a,'THREE',3,0));
  • CASE expects datatype consistency, DECODE does not.

  • CASE handles NULL differently.

  • CASE complies with ANSI SQL. DECODE is proprietary to Oracle.
  • CASE executes faster in the Optimizer than does DECODE. 
Based on above comparison, you can identify which one will be the best use for your requirement.


Get involved and leave your Comments in the Box Below. The more people get involved, the more we all benefit. So, leave your thoughts before you leave the page.

14 comments:

  1. Great blog on Oracle sql/plsql. Very nicely arranged.I too have written some articles on oracle decode and Oracle case statement. Please do checkout

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Facing error ("This site can’t be reached") if i click on any topic from Home page. Please someone help me. I'm reachable at selhar.reddyb2@gmail.com

    ReplyDelete
    Replies
    1. Hi Rajasekhar, Try now Blogger has changed it to .com from .in

      Delete
  5. This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. 連続殺人事件

    ReplyDelete
  6. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp a great deal more around this condition. and I could assume you are an expert on this subject. Same as your blog i found another one Oracle Fusion Cloud Technical .Actually I was looking for the same information on internet for Oracle Fusion Cloud Technical and came across your blog. I am impressed by the information that you have on this blog. Thanks a million and please keep up the gratifying work.

    ReplyDelete
  7. One thing, i still see people using Decode more compared to CASE, why it is so?

    I too have some questions collected from my side as well View vs Materialized View have a look

    ReplyDelete
  8. I'd like to express my gratitude for the time and work you put into creating this essay.
    I'm looking for more of the same from you in the future. IAPPC, a leading Netsuite Partner in India for ERP installation and solution provider, provides end-to-end implementation services and support to meet the organization's demands.

    ReplyDelete
  9. Awesome Blog! Thanks for sharing this post. It is an interesting post for everyone. When i read about this post then got more information. I like it.

    supplement shop near me UK

    cbd supplements near Kent

    mk-677 for sale kent

    ReplyDelete
  10. HELLO, THANK YOU FOR VISITING MY BLOG.

    ENJOYED READING MY ARTICLE?

    kindly Support by sharing this and making donation to :

    BITCOIN : bc1qgkncx8pfu24cn8gzf4wpqv7fk5v0nvp4wm95pk

    ETHER: 0x14e41D03e09Af44EeF505bb265C616075B5b668b

    SHIBA INU: 0x14e41D03e09Af44EeF505bb265C616075B5b668b

    BTT: 0x14e41D03e09Af44EeF505bb265C616075B5b668b


    OTHER CURRENCY TO OUR MULTI COIN WALLET :0x14e41D03e09Af44EeF505bb265C616075B5b668b

    ReplyDelete