• oracle通用函数,nvl,nvl2,NULLIF ,coalesce


     
    Oracle 通用函数 
    ① 
    NVL 函数--------将空值转换成一个已知的值 

    可以使用的数据类型有日期、字符、数字。 

    函数的一般形式:  
      NVL(commission_pct,0)           
      NVL(hire_date,'01-JAN-97')

      NVL(job_id,'No Job Yet') 

    (注意select 原数据类型是number  对返回值要求也是number  其他同理)? 

    求年薪  

    select employee_id,
           nvl(commission_pct,0) ,
          salary*12 ,
           salary*12*(1+nvl(commission_pct,0)) as "annual salary"--------------(commission_pct 有的没有) 
    
      from employees           

          

    练习2:输出last_name,department_id,当department_id为null时,显示‘没有部门’。-----------当时没思路 

    select employee_id 
           ,nvl(to_char(department_id),'没有部门') ----------------------------后面没法转 转前面 
    from employees   

     

    2. 使用 NVL2 函数 
    格式:NVL2 (expr1, expr2, expr3) : expr1不为NULL,返回expr2;为NULL,返回expr3。 

    练习:查询员工的奖金率,若为空,返回0.01,若不为空,返回实际奖金率+0.015 

    select  last_name 
            ,commission_pct 
            ,nvl2(commission_pct,commission_pct+0.05,0.01) from employees 

        

    3. 使用 NULLIF 函数 
     
    NULLIF (expr1, expr2) :  相等返回NULL,不等返回expr1  
     
    练习 当 first_name长度等于last_name长度 返回null 否则返回 first_name  

    select   length(first_name) ,
             length(last_name),
             nullif(length(first_name),
             length(last_name)) 
    from employees                


    4. 使用 COALESCE 函数

    更具有一般性   coalesce()的参数是可变参数。 参数要求2个以上都行。。 

    样式: 
    coalese(参数1,参数2)  ---------------当第一个参数为为null的时候  判断第二参数  若第二个也为空 返回空 
    coalese(参数1,参数2,参数3) ---------------第一个为空判断第二个,第二个为空判断第三个,第三个为空返回null

      
    COALESCE 与 NVL 相比的优点在于 COALESCE 可以同时处理交替的多个值。 

    如果第一个表达式为空,则返回下一个表达式,对其他的参数进行COALESCE  
     
    练习 判断commission_pct 是否为null

      ①否 commission_pct            

      ②是  判断salary是否为null

         ①否  就打印工资                            

         ②是  就打印 10  

     select   first_name ,
             commission_pct ,
             salary,
             coalesce(commission_pct,salary,100) 
        from employees   
      order by commission_pct         
  • 相关阅读:
    crontab机会任务监控
    Python 模块的一般处理
    MySQLdb autocommit
    MySQLdb callproc 方法
    Pthon MySQLdb 的安装
    CentOS7安装MySQL
    Linux中的网络
    Linux中的盘符问题
    类比的方法学习Performance_schema
    MySQL 设置数据库的隔离级别
  • 原文地址:https://www.cnblogs.com/hanxue53/p/8027092.html
Copyright © 2020-2023  润新知