Home > Ask the Oracle Database / Applications Experts > Questions & Answers > DECODE versus CASE, continued
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

DECODE versus CASE, continued

Frank Kulash EXPERT RESPONSE FROM: Frank Kulash

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 14 August 2003
This example illustrates the advantage of CASE over DECODE...

>

The next example illustrates the advantage of CASE over DECODE. It computes shipping charges based on a subtotal, according to these rules

  • Subtotal under $10.00:  Shipping is $3.00
  • $10.00 to $19.99:  $5.50
  • $20.00 to $99.99:  $7.50
  • $100.00 or more:  7% of subtotal (10% outside US)
DECODE Searched CASE
SELECT
  subtotal,
  country,
  DECODE
  (
    SIGN (subtotal - 20.00),
    -1,
    DECODE
    (
      SIGN (subtotal - 10.00),
      -1,
      3.00,
      5.50
    ),
    DECODE
    (
      SIGN (subtotal - 100.00),
      -1,
      7.50,
      DECODE
      (
        country,
        'US',
        .07,
        .10
      ) * subtotal
    )
  ) AS shipping
FROM    order ... ;
SELECT
  subtotal,
  country,
  CASE
    WHEN  subtotal <  10.00
      THEN  3.00
    WHEN  subtotal <  20.00
      THEN  5.50
    WHEN  subtotal < 100.00
      THEN  7.50
    WHEN  country = 'US'
      THEN  subtotal * .07
    ELSE  subtotal * .10
  END  AS shipping
FROM    order ... ;

CASE, like DECODE, works from left to right: it evaluates the WHEN-conditions in order, and as soon as it finds one that is true, it returns the corresponding THEN-value and quits. For example, in computing the shipping charges above, suppose the subtotal is 15.00. CASE will first test to see if subtotal is less than 10.00. It isn't, so CASE will move on to the next condition, and test if subtotal is less than 20.00. It is, so CASE immediately returns 5.50: it does not perform any of the remaining tests.

Anything you can do with CASE can be done in DECODE, but it's likely to be tortuous. CASE is almost always easier to read and understand, and therefore it's easier to debug and maintain.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts