Oracle 中 nvl、nvl2、nullif、coalesce、decode 函数的用法详解
NVL(EXPR1,EXPR2) NVL2(EXPR1,EXPR2,EXPR3) NULLIF(EXPR1,EXPR2) COALESCE(EXPR1,,..,EXPRn) decode --------NVL nvl(COMMISSION_PCT,0) 如果第一个参数为null,则返回第二个参数 如果第一个参数为非null,则返回第一个参数 经典示例:计算年薪(工资提成
NVL(EXPR1,EXPR2)NVL2(EXPR1,EXPR2,EXPR3)
NULLIF(EXPR1,EXPR2)
COALESCE(EXPR1,,..,EXPRn)
decode
--------NVL
nvl(COMMISSION_PCT,0)
如果第一个参数为null,则返回第二个参数
如果第一个参数为非null,则返回第一个参数
经典示例:计算年薪(工资+提成)
SELECT LAST_NAME,SALARY,NVL(COMMISSION_PCT,0),
(SALARY*12)+(SALARY*12*NVL(COMMISSION_PCT,0)) ANNUAL_SALARY
FROM EMPLOYEES
WHERE LAST_NAME='Matos';
--------NVL2
NVL2(COMMISSION_PCT,'SAL_COMM','SAL')
如果第一个参数为null,则返回第三个参数
如果第一个参数为非null,则返回第二个参数
经典示例:
SELECT LAST_NAME,SALARY,COMMISSION_PCT,
NVL2(COMMISSION_PCT,'SAL+COMM','SAL') INCOME
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (50,80);
---------NULLIF
nullif(length(first_name),length(last_name))
如果两个参数值相等,则返回 null
如果两个参数不等,则返回第一个表达式值
SELECT FIRST_NAME ,LENGTH(FIRST_NAME) "EXPR1",
LAST_NAME, LENGTH(LAST_NAME) "expr2",
nullif(length(first_name),length(last_name)) result
FROM EMPLOYEES;
---------COALESCE
COALESCE(EXPR1,EXPR2,EXPR3...EXPRn)
从左往右数,遇到第一个非null值,则返回该非null值。
多层判断
SELECT LAST_NAME,EMPLOYEE_ID,
COALESCE(TO_CHAR(commission_pct),TO_CHAR(manager_id),
'No commission and no manager')
FROM EMPLOYEES;
---------------------------------
-----------条件表达式
IF-THEN-ELSE
CASE 语句(SQL标准,编写繁杂)
DECODE 函数
DECODE(COL|EXPRESSION,SERACH1,RESULT1
[,search2,result2,...,]
[,default])
如果 search1 = expression 则返回 result1
如果 search2 = expression 则返回 result2
---------CASE
示例:
SELECT LAST_NAME,JOB_ID,SALARY,
CASE JOB_ID WHEN 'IT_PROG' THEN 1.10*SALARY
WHEN 'ST_CLERK' THEN 1.15*SALARY
WHEN 'SA_REP' THEN 1.20*SALARY
ELSE SALARY END "REVISED SALARY"
FROM EMPLOYEES;
---------DECODE
示例:
DECODE函数的作用:它可以将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。函数的参数列表是由若干数值及其对应结果值组成的若干序偶形式。当然,如果未能与任何一个实参序偶匹配成功,则函数也有默认的返回值。
区别于SQL的其它函数,DECODE函数还能识别和操作空值。
语法如下:
DECODE(control_value,value1,result1[,value2,result2…][,default_result]);
control _value
试图处理的数值。DECODE函数将该数值与后面的一系列的偶序相比较,以决定返回值。
value1
是一组成序偶的数值。如果输入数值与之匹配成功,则相应的结果将被返回。对应一个空的返回值,可以使用关键字NULL于之对应
result1
是一组成序偶的结果值。
default_result 未能与任何一个值匹配时,函数返回的默认值。
示例如下:
select decode( x , 1 , ‘x is 1 ’, 2 , ‘x is 2 ’, ‘others’) from dual
http://blog.csdn.net/xiangsir/article/details/8601513

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.
