<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-26  評論-13  文章-46  trackbacks-0

    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).


    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)


    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;

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

    posted on 2006-04-04 12:51 似水流年 閱讀(460) 評論(0)  編輯  收藏 所屬分類: Oracle
    主站蜘蛛池模板: 91麻豆国产自产在线观看亚洲| 日韩一级免费视频| 亚洲色欲色欲www| 免费在线看污视频| 成人免费激情视频| 亚洲精品日韩专区silk| 国产精彩免费视频| 亚洲一区二区三区精品视频| 亚洲免费综合色在线视频| 亚洲天堂中文字幕在线| 一级毛片人与动免费观看| 天堂在线免费观看中文版| 亚洲熟妇无码乱子AV电影| 国产99视频精品免费视频76| 夫妻免费无码V看片| 99亚洲男女激情在线观看| 91在线品视觉盛宴免费| 亚洲熟妇无码八V在线播放| 青草草在线视频永久免费| 在线观看亚洲免费| 中文字幕亚洲日韩无线码| 精品韩国亚洲av无码不卡区| 免费在线一级毛片| 四虎精品成人免费视频| 亚洲av无码乱码国产精品fc2| 精品女同一区二区三区免费站| 亚洲午夜精品国产电影在线观看| 成全高清在线观看免费| 久久精品国产亚洲AV无码麻豆| 中国在线观看免费高清完整版| 亚洲精品国产suv一区88| 亚洲综合国产一区二区三区| 美丽姑娘免费观看在线观看中文版| 亚洲一区电影在线观看| 亚洲国产高清精品线久久| 色九月亚洲综合网| 日本亚洲欧洲免费天堂午夜看片女人员 | 亚洲av无码专区在线播放| 毛片免费视频播放| 亚洲日韩中文在线精品第一| 无码午夜成人1000部免费视频|