Oracle function usage tips (1)-Decode

Source: Internet
Author: User

Functions in Oracle (1)-The Decode ecode () function is one of the powerful functions of oracle pl/SQL. Currently, only oracle SQL provides this function, this function is not available for SQL implementations of other database vendors. The DECODE function is one of the powerful functions of oracle pl/SQL. Currently, only oracle SQL provides this function, and other database vendors do not yet implement this function. What is the purpose of DECODE? First, let's construct an example. If we want to add a salary to a staff member of zhixing, the standard is: the salary of less than 8000 yuan will be increased by 20%; the salary of more than 8000 yuan will be increased by 15%, usually, select the salary field value in the record first? Select salary into var-salary from employee, and then use if-then-else or choose case to judge the variable var-salary. If the DECODE function is used, we can omit these flow control statements and directly complete them through SQL statements. Select decode (sign (salary-8000), 1, salary * 1.15,-1, salary * 1.2, salary from employee is very concise? DECODE Syntax: DECODE (value, if1, then1, if2, then2, if3, then3 ,..., else), indicating that if the value is equal to if1, the result of the DECODE function returns then1 ,..., if it is not equal to any if value, else is returned. At first glance, DECODE can only perform equals tests, but as we have seen just now, we can use some functions or computing to replace value to enable the DECODE function to have the functions greater than, less than, or equal. DECODE (condition, value 1, translation value 1, value 2, translation value 2 ,... value n, translation value n, default value) DECODE (field, comparison 1, value 1, comparison 2, value 2 ,....., compare n, value n default value) the meaning of this function is as follows: IF condition = value 1 then return (translation value 1) ELSIF condition = value 2 then return (translation value 2 )...... ELSIF condition = value n then return (translation value n) else return (default value) end if decode () function usage tips · software environment: 1. Windows NT4.0 + ORACLE 8.0.4 2. ORACLE installation path: C:/ORANT. Usage: 1. Compare the size of select decode (sign (variable 1-variable 2 ), -1, variable 1, variable 2) from dual; -- take a small value. The sign () function is based on a value of 0, positive number, or negative number, return 0, 1,-1, for example, variable 1 = 10, variable 2 = 20 then sign (variable 1-variable 2) returns-1, and the decode decoding result is "variable 1", achieving the goal of getting a smaller value. 2. the table and view structure are converted into the current sales table sale. The table structure is as follows:

Month char (6) -- month limit number () -- The current monthly sales amount data is: 200001 1000 200002 1100 200003 1200 200004 1300 200005 1400 200006 1500 200007 1600 200101 1100 200202 1200 200301 data to be converted to the following structure: year char (4) -- year month1 number (1300) -- sales amount for June: monthnumber () -- sales amount for June: month3 number () -- sales amount for June: month4 number () -- sales amount for June: month5 number) -- sales amount month6 number () -- sales amount month7 number () -- 7 Monthly sales amount: month8 number () -- August sales amount: month9 number () -- September sales amount: month10 number () -- October sales amount: month11 number) -- sales amount month12 number () -- the SQL statement for converting the sales amount structure in is: create or replace view v_sale (year, month1, mon2, month3, month4, month5, month6, month7, month8, month9, month10, month11, month12) as select substrb (month,), sum (decode (substrb (month, 5, 2), '01', region, 0), sum (decode (substrb (month, 5, 2), '0 2', substring, 0), sum (decode (substrb (month, 5, 2), '03', substring, 0), sum (decode (substrb (month, 5, 2), '04 ', average, 0 )), ========================================================== ================= Supplement 1: students whose student ID table is student must use the decode function to implement the following functions: score> 85, excellent;> 70: Good;> 60: pass; otherwise, fail. Suppose student is id and score is score, then: select id, decode (sign (score-85), 1, 'out', 0, 'out',-1, decode (sign (score-70), 1, 'good', 0, 'good',-1, decode (sign (score-60), 1, 'pass ', 0, 'pass',-1, 'failed') from student; ========================================================== ================= Supplement 2: the syntax structure of the Decode function is as follows: decode (expression, search_1, result_1) decode (expression, search_1, result_1, search_2, result_2) decode (expression, search_1, result_1, search_2, result_2 ,...., search_n, result_n) decode (expression, search_1, result_1, default) decode (expression, search_1, result_1, search_2, result_2, default) decode (expression, search_1, result_1, search_2, result_2 ,...., search_n, result_n, default)

 

The comparison expression and search word of the decode function. If the expression matches, the return result is returned. If the expression does not match, the default value is returned. If the default value is not defined, the return value is null. The following is a simple test to Decode the function usage:
SQL> create table t as select username,default_tablespace,lock_date from dba_users;Table created.SQL> select * from t;USERNAME                        DEFAULT_TABLESPACE              LOCK_DATE------------------------------ ------------------------------ ---------SYS                             SYSTEMSYSTEM                          SYSTEMOUTLN                           SYSTEMCSMIG                           SYSTEMSCOTT                           SYSTEMEYGLE                           USERSDBSNMP                          SYSTEMWMSYS                           SYSTEM                          20-OCT-048 rows selected.SQL> select username,decode(lock_date,null,'unlocked','locked') status from t;USERNAME                        STATUS------------------------------ --------SYS                             unlockedSYSTEM                          unlockedOUTLN                           unlockedCSMIG                           unlockedSCOTT                           unlockedEYGLE                           unlockedDBSNMP                          unlockedWMSYS                           locked8 rows selected.SQL> select username,decode(lock_date,null,'unlocked') status from t;USERNAME                        STATUS------------------------------ --------SYS                             unlockedSYSTEM                          unlockedOUTLN                           unlockedCSMIG                           unlockedSCOTT                           unlockedEYGLE                           unlockedDBSNMP                          unlockedWMSYS8 rows selected.

 


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.