`
yunzhongxia
  • 浏览: 648474 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Decode Function

阅读更多

In Oracle/PLSQL, the decode function has the functionality of an IF-THEN-ELSE statement.

The syntax for the decode function is:

decode( expression , search , result [, search , result]... [, default] )

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 will return default. If default is omitted, then the decode statement will return null (if no matches are found).

 

Applies To:

  • Oracle 9i, Oracle 10g, Oracle 11g

 

For example:

You could use the decode function in an 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


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: 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')


Question:  I would like to know if it's possible to use decode 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 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.

分享到:
评论

相关推荐

    BlueskyFRC_2.15.3_Setup.exe

    AMD Fluid Motion Video是一项AMD研发,将帧率为24FPS【即帧每秒,Frames per Second】或者其他帧率低于60FPS的视频补帧成60帧的...Fixed error on the PotPlayer when enabling decode function using external decoder

    oracle_function_decode.patch

    博客:PostgreSQL的学习心得和知识总结(四十四)|语法级自上而下完美实现Oracle数据库DECODE函数的实现方案(GreenPlum & AntDB)

    base64decode

    base64decode 编码,解码。

    Sakemail

    A obscure bug was found by HuangYeJun from china, in the RetrieveHeaders function if the retrieved text was larger than 1024 bytes and the crlf.crlf fall in the middle of two chunks, the function is ...

    编程(db2)使用技巧

    #### 1.5 解码函数(DECODE Function) DECODE函数用于根据表达式的值返回不同的结果,类似于多路分支选择。例如: ```sql DECODE(column_name, 'value1', result1, 'value2', result2, default_result); ``` #### ...

    jwt-decode.js

    jwt JS解密文件 var getUserjwt = function (token) { if (!token) { return; } } //解析jwt令牌 var jwtDecodeVal = jwt_decode(token);

    JS 版本的base64函数实现(base64encode,base64decode)

    用js实现的base64encode,base64decode函数. 包括: function base64encode(str) { function base64decode(str) { function utf16to8(str) { function utf8to16(str) { function doit() {

    optimal:python元启发式优化库。 目前支持遗传算法,引力搜索,交叉熵和PBIL

    最佳(测试版) python元启发式优化库。 易于扩展和使用。 警告:最佳状态为Beta。 API可能会更改。...# A decode function is useful for converting the binary solution to real numbers def dec

    jQuery JSON with PHP json_encode and json_decode

    $.getJSON('server_script.php', function(data) { console.log(data.name); // 输出:John Doe }); ``` 这里,`$.getJSON`会发起一个HTTP GET请求到'server_script.php',服务器响应的JSON数据会被自动解析并作为...

    allwinner 全志 a10 Video Decode Source code

    全志 a10开发板 专用的 video 硬件 decoding 源码 video decoding sample source code using allwinner a10 hardware encoding function for development board (h264, h263, mp4,...)

    php也需要配置环境变量 解决Fatal error Call to undefined function curl_init()

    不用重启电脑.txt”可能包含了详细步骤来指导你如何添加环境变量,而“1.Fatal error Call to undefined function curl_init().txt”可能是对错误的详细描述或者解决方案的记录。“2.php.ini中extension = ...

    用ASP应用程序实现自己的UrlDeCode

    function urldecode(encodestr) newstr = "" havechar = false lastchar = "" for i = 1 to len(encodestr) char_c = mid(encodestr, i, 1) if char_c = "+" then newstr = newstr & " " elseif char_c = "%...

    js中encode、decode的应用说明.docx

    JavaScript中的`encode`和`decode`是一组用于字符串编码解码的方法,它们在处理和传输数据时起到关键作用。在JavaScript中,常见的编码方法有`encodeURIComponent`、`decodeURIComponent`、`encodeURI`、`decodeURI`...

    PHP实现json_decode不转义中文的方法

    然而,默认情况下,`json_decode` 会转义特殊字符并把非ASCII的中文字符转换为Unicode编码,这可能导致在数据库或日志中查看时不方便。本文将详细介绍如何在PHP中使用`json_decode` 不转义中文的方法。 对于PHP 5.4...

    ASP中实现的URLEncode、URLDecode自定义函数__1.docx

    Function URLDecode(enStr) dim deStr, strSpecial, c, i, v deStr = "" strSpecial = "!""#$%'()*+,.-_/:;=?@[/]^`{|}~%" For i = 1 To Len(enStr) c = Mid(enStr, i, 1) If c = "%" Then v = Eval("h" & ...

    ASP中只有UrlEncode,没有Urldecode问题的解决方法?

    Function URLDecode(enStr) Dim deStr Dim c, i, v deStr = "" For i = 1 To Len(enStr) c = Mid(enStr, i, 1) If c = "%" Then v = Eval("&h" + Mid(enStr, i + 1, 2)) If v deStr = deStr & Chr(v) i =...

    Oracle的几个Function实例

    在"Oracle的几个Function实例"这个主题中,我们将探讨一些常见的Oracle函数,并通过实际示例来理解它们的用法。 1. ** NVL 函数**: NVL函数用于处理空值(NULL)。如果一个字段的值为NULL,NVL函数会将其替换为你...

    用JavaScript实现PHP的urlencode与urldecode函数

    function js_urldecode(url) { var output = url; var binVal, thisString; var myregexp = /(%[^%]{2})/; while ((match = myregexp.exec(output)) != null) { // ... // 解码过程 // ... } function utf...

    解决Keras中循环使用K.ctc_decode内存不释放的问题

    decode = K.function([model.input, input_length], [ctc_decode[0][0]]) ``` 这样,`decode`函数就可以在循环中重复调用,而不会每次都向计算图添加新节点,从而解决了内存不释放的问题。 关于CTC_loss,同样的...

    php自定义urlencode,urldecode函数实例

    function ajax_decode($str){ $patern = array("/%25/","/%26/","/%3D/"); $rp = array("%","=","&"); return preg_replace($patern, $rp, $str); } ``` 通过这两个自定义函数,我们可以根据需要对字符串进行...

Global site tag (gtag.js) - Google Analytics