coalesce nvl nvl2 nullif

 
1、NVL (expr1, expr2)
->expr1为NULL,返回expr2;不为NULL,返回expr1。
注意:两者的类型要一致
例:
SQL> select nvl(e.student_code,2008), e.student_name,e.score  from aa e ;

NVL(E.STUDENT_CODE,2008) STUDENT_NAME              SCORE                       
------------------------ -------------------- ----------                       
                    2008 Jack Chen                   110                       
                    2008 Barack Hussin Obama                                   
                    1001 张三                         95                       
                    1008 Jay Chou                     80                       
                    1005 Bruce Lee                    90                       
                    1006 Jack Lee                     82                       
                    2008                             100                       
                    1000 Bruce Lee                   100                       

已选择8行。

2、NVL2 (expr1, expr2, expr3)
->expr1不为NULL,返回expr2;为NULL,返回expr3。
expr2和expr3类型不同的话,expr3会转换为expr2的类型
例:
SQL> select nvl2(e.student_code,'80','Hussin'),e.student_name,e.score
  2  from aa e ;

NVL2(E STUDENT_NAME              SCORE                                         
------ -------------------- ----------                                         
Hussin Jack Chen                   110                                         
80     Barack Hussin Obama                                                     
80     张三                         95                                         
80     Jay Chou                     80                                         
80     Bruce Lee                    90                                         
80     Jack Lee                     82                                         
Hussin                             100                                         
80     Bruce Lee                   100                                         

已选择8行。

3、NULLIF (expr1, expr2)
->相等返回NULL,不等返回expr1
例:
SQL> select nullif(e.student_code,2008), e.student_name,e.score  from aa e ;

NULLIF(E.STUDENT_CODE,2008) STUDENT_NAME              SCORE                    
--------------------------- -------------------- ----------                    
                            Jack Chen                   110                    
                            Barack Hussin Obama                                
                       1001 张三                         95                    
                       1008 Jay Chou                     80                    
                       1005 Bruce Lee                    90                    
                       1006 Jack Lee                     82                    
                                                        100                    
                       1000 Bruce Lee                   100                    

已选择8行。

 

4、COALESCE(exp1,exp2,...)
COALESCE和NULLIF 函数 这两个函数都和NULL的处理有关系.其语法分别如下:

COALESCE(exp1,exp2,...)在所有的表达式中返回第一个不为null的结果.等价于下面的CASE语句:
  CASE
    when exp1 is not null then exp1
    when exp2 is not null then exp2
    ..
    else null

当找到第一个非null的表达式后,这个表达式的结果就会返回,而不会继续计算后面的表达式了。如果所有的表示式都为null的时候,这个函数的结果也是null,这是很自然的结果但是也有例外(那就是所有的表达式都是直接的null,如:COALESCE(null,null))。

例:
SQL> select coalesce(e.student_code,e.score,8) , e.student_name,e.score  from aa e;

COALESCE(E.STUDENT_CODE,E.SCORE,8) STUDENT_NAME              SCORE             
---------------------------------- -------------------- ----------             
                               110 Jack Chen                   110             
                              2008 Barack Hussin Obama                         
                              1001 张三                         95             
                              1008 Jay Chou                     80             
                              1005 Bruce Lee                    90             
                              1006 Jack Lee                     82             
                               100                             100             
                              1000 Bruce Lee                   100             

已选择8行。

NULLIF函数的函数是在两个参数相等的时候返回NULL,否则返回第一个参数,NULLIF也可以用CASE来解释:
 CASE
    when exp1 =exp2 then null
    else exp1


报表函数:
1、isIn(val,vals)
->判断给定值是否在一个值集里.返回:布尔值

2、null()
->返回一null对象.返回:null

你可能感兴趣的:(coalesce nvl nvl2 nullif)