Decode anatomy of Oracle function usage

Source: Internet
Author: User

Decode for Oracle function usage. The DECODE function compares the value with a series of sequence dolls to determine the final returned results. We compared the Oracle function with the switch function and found that they are basically similar. The only difference is that the switch will compare the values with the break after the case.

In the following example, I found it on the Internet)

 
 
  1. SELECT id,  
  2. DECODE(flag,’Y’,'Yes’,'N’,’No’,NULL,’None’,'Yes’)  
  3. FROM t_test;  

If the flag field of t_test is Y, Yes is returned. If it is N, No is returned. If it is NULL, None is returned. Yes is returned by default.

Let's take a look at my problems:

 
 
  1. SELECT a. id,
  2. A. user_name,
  3. A. oper_time time2,
  4. A. oper_type,
  5. A. description,
  6. A. oper_flag,
  7. Decode (a. oper_flag, '1', 'successfully', 'failed') oper_flag,
  8. A. ip,
  9. B. true_name
  10. FROM SEC_LOG_OPERATION
  11. Left join sec_user B on a. user_name = B. user_name
  12. Where 11 = 1
  13. Order by oper_time desc

The original red lines are: decode (a. oper_flag, 1, 'success', 'failed ')

I will report an error during the query, because the oper_flag field contains non-numeric data, that is, this field not only has 1, 0, but also "failed". This data! = Character. So we can process 1 as a character. The above content is an introduction to the decode of Oracle function usage. I hope you will find some gains.

Related Article

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.