Summary of Oracle case usage

Source: Internet
Author: User

The following articles mainly introduce the actual usage of Oracle case. We all know that the case expression can be used in SQL to implement the actual application logic of the if-then-else type, PL/SQL does not have to be used. Oraclecase works in a similar way as DECODE (), but case should be used because it is ANSI compatible.

Case has two expressions:

1. Use a simple Oraclecase expression to determine the return value.

Syntax:

 
 
  1. case search_expression  
  2. WHEN expression1 THEN result1  
  3. WHEN expression2 THEN result2  
  4. ...  
  5. WHEN expressionN THEN resultN  
  6. ELSE default_result  
  7. END  

Example:

 
 
  1. select product_id,product_type_id,  
  2. case product_type_id  
  3. when 1 then 'Book'  
  4. when 2 then 'Video'  
  5. when 3 then 'DVD'  
  6. when 4 then 'CD'  
  7. else 'Magazine'  
  8. end  
  9. from products  

Result:

PRODUCT_ID PRODUCT_TYPE_ID OraclecasePROD

---------------------------------

1 Book

2 1 Book

3 2 Video

4 2 Video

5 2 Video

6 2 Video

7 3 DVDs

8 3 DVDs

9 4 CD

10 4 CD

11 4 CD

12 Magazine

12 rows selected.

2. Search for the case expression and use the condition to determine the return value.

Syntax:

 
 
  1. case  
  2. WHEN condition1 THEN result1  
  3. WHEN condistion2 THEN result2  
  4. ...  
  5. WHEN condistionN THEN resultN  
  6. ELSE default_result  
  7. END  

Example:

 
 
  1. select product_id,product_type_id,  
  2. Oraclecase  
  3. when product_type_id=1 then 'Book'  
  4. when product_type_id=2 then 'Video'  
  5. when product_type_id=3 then 'DVD'  
  6. when product_type_id=4 then 'CD'  
  7. else 'Magazine'  
  8. end  
  9. from products  

The result is the same as above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.