通用函数主要有两个:NVL(),DECODE(),这两个函数算是ORACLE自己的特色函数;

1.NVL()函数,处理 NULL

例子:查出每个雇员的全部年薪;

SQL> SELECT first_name,salary,commission_pct,(salary+commission_pct)*12 FROM employees;

FIRST_NAME               SALARY COMMISSION_PCT (SALARY+COMMISSION_PCT)*12
-------------------- ---------- -------------- --------------------------
Donald                     2600
Douglas                    2600
Jennifer                   4400
Michael                   13000
Pat                        6000
Susan                      6500
Hermann                   10000
Shelley                   12000
William                    8300
Steven                    24000
Neena                     17000

FIRST_NAME               SALARY COMMISSION_PCT (SALARY+COMMISSION_PCT)*12
-------------------- ---------- -------------- --------------------------
Lex                       17000
Alexander                  9000
Bruce                      6000
David                      4800
Valli                      4800
Diana                      4200
Nancy                     12000
Daniel                     9000
John                       8200
Ismael                     7700
Jose Manuel                7800

FIRST_NAME               SALARY COMMISSION_PCT (SALARY+COMMISSION_PCT)*12
-------------------- ---------- -------------- --------------------------
Luis                       6900
Den                       11000
Alexander                  3100
Shelli                     2900
Sigal                      2800
Guy                        2600
Karen                      2500
Matthew                    8000
Adam                       8200
Payam                      7900
Shanta                     6500

FIRST_NAME               SALARY COMMISSION_PCT (SALARY+COMMISSION_PCT)*12
-------------------- ---------- -------------- --------------------------
Kevin                      5800
Julia                      3200
Irene                      2700
James                      2400
Steven                     2200
Laura                      3300
Mozhe                      2800
James                      2500
TJ                         2100
Jason                      3300
Michael                    2900

FIRST_NAME               SALARY COMMISSION_PCT (SALARY+COMMISSION_PCT)*12
-------------------- ---------- -------------- --------------------------
Ki                         2400
Hazel                      2200
Renske                     3600
Stephen                    3200
John                       2700
Joshua                     2500
Trenna                     3500
Curtis                     3100
Randall                    2600
Peter                      2500
John                      14000             .4                   168004.8

FIRST_NAME               SALARY COMMISSION_PCT (SALARY+COMMISSION_PCT)*12
-------------------- ---------- -------------- --------------------------
Karen                     13500             .3                   162003.6
Alberto                   12000             .3                   144003.6
Gerald                    11000             .3                   132003.6
Eleni                     10500             .2                   126002.4
Peter                     10000             .3                   120003.6
David                      9500            .25                     114003
Peter                      9000            .25                     108003
Christopher                8000             .2                    96002.4
Nanette                    7500             .2                    90002.4
Oliver                     7000            .15                    84001.8
Janette                   10000            .35                   120004.2

这时有雇员的年薪变成了NULL,而造成这种问题的关键是在commission_pct字段上位NULL,那我们解决这个问题就必须做一种处理:将NULL变成“0”

SQL> SELECT first_name,salary,commission_pct,(salary+NVL(commission_pct,0))*12 FROM employees;

FIRST_NAME               SALARY COMMISSION_PCT (SALARY+NVL(COMMISSION_PCT,0))*12
-------------------- ---------- -------------- ---------------------------------
Donald                     2600                                            31200
Douglas                    2600                                            31200
Jennifer                   4400                                            52800
Michael                   13000                                           156000
Pat                        6000                                            72000
Susan                      6500                                            78000
Hermann                   10000                                           120000
Shelley                   12000                                           144000
William                    8300                                            99600
Steven                    24000                                           288000
Neena                     17000                                           204000

注意:NVL就是将NULL变为0.

2.DECODE()函数:多数值判断

DECODE()函数非常类似于程序中的if。。。else语句,不同的是DECODE()函数判断是数值,而不是逻辑条件。

例子:将雇员的职位替换成中文显示:

SH_CLERK:办事员

这种判断肯定是逐行判断,所以这时就必须用到DECODE()函数,而此时函数语法如下:

DECODE(数值 |列,判断值1,显示值1,判断值2,显示值2,判断值3,显示值3,...)

例子;实现显示操作功能

SQL> SELECT first_name,job_id,DECODE(job_id,'SH_CLERK','办事员') FROM employees;

FIRST_NAME           JOB_ID     DECODE
-------------------- ---------- ------
Donald               SH_CLERK   办事员
Douglas              SH_CLERK   办事员

注意:DECODE()函数是我们ORACLE中最有特点的函数,一定要掌握。

4.总结

SQL的基本语法是SELECT 、FROM、WHERE、ORADER BY 的关系;

HR用户的所有数据表的信息,包括列的名称,即作用和类型;

ORACLE中的几个单行函数,要记下来所有的函数都要求会使用;