zoukankan      html  css  js  c++  java
  • Oracle排序中NULL值处理的五种常用方法

    1、缺省处理
     
    Oracle在Order by 时缺省认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前
    2、使用nvl函数
     
    nvl函数可以将输入参数为空时转换为一特定值,如
    nvl(employee_name,’张三’)表示当employee_name为空时则返回’张三’,如果不为空则返回employee_name
    通过这个函数可以定制null的排序位置。
    3、使用decode函数
     
    decode函数比nvl函数更强大,同样它也可以将输入参数为空时转换为一特定值,如
    decode(employee_name,null,’张三’, employee_name)表示当employee_name为空时则返回’张三’,如果不为空则返回employee_name
    通过这个函数可以定制null的排序位置。
    4、使用case 语法
     
    Case语法是Oracle 9i后开始支持的,是一个比较灵活的语法,同样在排序中也可以应用
    如:
    select *
     from employee
     order by  (case employee_name
                when null then
                 '张三'
                else
                 employee_name
              end)
    表示当employee_name为空时则返回’张三’,如果不为空则返回employee_name
    通过case语法同样可以定制null的排序位置。
     
    5、使用nulls first 或者nulls last 语法
     
    Nulls first和nulls last是Oracle Order by支持的语法
    如果Order by 中指定了表达式Nulls first则表示null值的记录将排在最前(不管是asc 还是 desc)
    如果Order by 中指定了表达式Nulls last则表示null值的记录将排在最后 (不管是asc 还是 desc)
    使用语法如下:
    --将nulls始终放在最前
    select * from zl_cbqc order by cb_ld nulls first
     
    --将nulls始终放在最后
    select * from zl_cbqc order by cb_ld desc nulls last
  • 相关阅读:
    《C# to IL》第一章 IL入门
    multiple users to one ec2 instance setup
    Route53 health check与 Cloudwatch alarm 没法绑定
    rsync aws ec2 pem
    通过jvm 查看死锁
    wait, notify 使用清晰讲解
    for aws associate exam
    docker 容器不能联网
    本地运行aws lambda credential 配置 (missing credential config error)
    Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
  • 原文地址:https://www.cnblogs.com/Han-org/p/7563102.html
Copyright © 2011-2022 走看看