设为首页 收藏本站
查看: 813|回复: 0

[经验分享] oracle decode function-bckong

[复制链接]

尚未签到

发表于 2018-9-13 07:07:04 | 显示全部楼层 |阅读模式
Oracle/PLSQL: DECODE Function
  The Oracle/PLSQL DECODE function has the functionality of an IF-THEN-ELSE statement.
Oracle DECODE Syntax
  The syntax for the Oracle/PLSQL DECODE function is:
DECODE( expression , search , result [, search , result]... [, default] )Parameters or Arguments
  expression is the value to compare.
  search is the value that is compared against expression.
  result is the value returned, if expression is equal to search.
  default is optional. If no matches are found, the DECODE function will return default. If default is omitted, then the DECODE function will return null (if no matches are found).
Applies To
  The DECODE function can be used in the following versions of Oracle/PLSQL:

  •   Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i
Oracle DECODE Example
  The DECODE function can be used in Oracle/PLSQL.
  You could use the DECODE function in a SQL statement as follows:
SELECT supplier_name,  
DECODE(supplier_id, 10000, 'IBM',
  10001, 'Microsoft',
  10002, 'Hewlett Packard',
  'Gateway') result
  
FROM suppliers;
  The above DECODE statement is equivalent to the following IF-THEN-ELSE statement:
IF supplier_id = 10000 THEN  result := 'IBM';
  

  
ELSIF supplier_id = 10001 THEN
  result := 'Microsoft';
  

  
ELSIF supplier_id = 10002 THEN
  result := 'Hewlett Packard';
  

  
ELSE
  result := 'Gateway';
  

  
END IF;
  The DECODE function will compare each supplier_id value, one by one.
Frequently Asked Questions (DECODE Function)
  Question: One of our viewers wanted to know how to use the DECODE function to compare two dates (ie: date1 and date2), where if date1 > date2, the DECODE function should return date2. Otherwise, the DECODE function should return date1.
  Answer: To accomplish this, use the DECODE function as follows:
DECODE((date1 - date2) - abs(date1 - date2), 0, date2, date1)  The formula below would equal 0, if date1 is greater than date2:
(date1 - date2) - abs(date1 - date2)  Helpful Tip #1: One of our viewers suggested combining the SIGN function with the DECODE function as follows:
  The date example above could be modified as follows:
DECODE(SIGN(date1-date2), 1, date2, date1)  The SIGN/DECODE combination is also helpful for numeric comparisons e.g. Sales Bonuses
DECODE(SIGN(actual-target), -1, 'NO Bonus for you', 0,'Just made it', 1, 'Congrats, you are a winner')  Helpful Tip #2: One of our viewers suggested using the LEAST function (instead of the DECODE function) as follows:
  The date example above could be modified as follows:
LEAST(date1, date2)  Question: I would like to know if it's possible to use the DECODE function for ranges of numbers, ie 1-10 = 'category 1', 11-20 = 'category 2', rather than having to individually decode each number.
  Answer: Unfortunately, you can not use the DECODE function for ranges of numbers. However, you can try to create a formula that will evaluate to one number for a given range, and another number for the next range, and so on.
  For example:
SELECT supplier_id,  
DECODE(TRUNC ((supplier_id - 1) / 10), 0, 'category 1',
  1, 'category 2',
  2, 'category 3',
  'unknown') result
  
FROM suppliers;
  In this example, based on the formula:
TRUNC ((supplier_id - 1) / 10  The formula will evaluate to 0, if the supplier_id is between 1 and 10.
  The formula will evaluate to 1, if the supplier_id is between 11 and 20.
  The formula will evaluate to 2, if the supplier_id is between 21 and 30.
  and so on...
  Question: I need to write a DECODE statement that will return the following:
If yrs_of_service < 1 then return 0.04  
If yrs_of_service >= 1 and < 5 then return 0.04
  
If yrs_of_service > 5 then return 0.06
  How can I do this?
  Answer: You will need to create a formula that will evaluate to a single number for each one of your ranges.
  For example:
SELECT emp_name,  
DECODE(TRUNC (( yrs_of_service + 3) / 4), 0, 0.04,
  1, 0.04,
  0.06) as perc_value
  
FROM employees;
  Question: Is there a limit to the number of arguments that you can have in one DECODE statement?  I'm getting an error, "ORA-00939: too many arguments for function".
  Answer: Yes, the maximum number of components that you can have in a DECODE function is 255. This includes the expression, search, and result arguments.
  原文出处:http://www.techonthenet.com/oracle/functions/decode.php
  ps:小弟翻译水平有限,就不献丑了,我也是大部分看得明白的,相信大家也会明白的。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-577236-1-1.html 上篇帖子: Oracle中使用同义词 下篇帖子: oracle引起一些问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表