---
title: "oracle 空值处理，排序过滤"
author: "Caraly"
date: "2013-09-25T11:05:30Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2013-10-30T10:10:46Z"
id: 1946977
url: "https://caraly.iteye.com/blog/1946977"
---

# [oracle 空值处理，排序过滤](http://www.cnblogs.com/legend_sun/archive/2009/03/20/1417429.html)

**oracle认为 null 最大。**

**升序排列，**默认情况下，null值排后面。

**降序排序，**默认情况下，null值排前面。

有几种办法改变这种情况：

（1）用 nvl 函数或decode 函数 将null转换为一特定值

（2）用case语法将null转换为一特定值（oracle9i以后版本支持。和sqlserver类似）：  
order by (case mycol when null then ’北京漂客’     else   mycol   end)

（3）使用nulls first 或者nulls last 语法。

这是oracle专门用来null值排序的语法。

nulls first ：将null排在最前面。如：select * from mytb order by mycol nulls first

null last ：将null排在最后面。如：select * from mytb order by mycol nulls last

 

【sqlserver】：

**sqlserver 认为 null 最小。**

**升序排列**：null 值默认排在最前。

要想排后面，则：order by case when col is null then 1 else 0 end ,col

**降序排列**：null 值默认排在最后。

要想排在前面，则：order   by case when col is null then 0 else 1 end , col desc

说明：  
1、等价于没有任何值、是未知数。  
2、NULL与0、空字符串、空格都不同。  
3、对空值做加、减、乘、除等运算操作，结果仍为空。  
4、NULL的处理使用NVL函数或者nvl2。  
5、比较时使用关键字用“is null”和“is not null”。  
6、空值不能被索引，所以查询时有些符合条件的数据可能查不出来，  
   count(\*)中，用nvl(列名,0)处理后再查。  
7、排序时比其他数据都大（索引默认是降序排列，小→大），  
   所以NULL值总是排在最后。

对null 值的比较只能是is , is not ， null通过其他方式和任何值（包括null）的比较结果都是空  
对null值的处理可以通过nvl(,)

select * from dual where dummy is null;

 推荐下朋友开的淘宝店筱夜宫主（[http://xiaoyegongzhu.taobao.com/](http://xiaoyegongzhu.taobao.com/)）DR.HU 胡博士美妆~坐在电脑旁的汉子姑娘注意脸部护理呀，做了程序媛，也要美丽啊。。。小店有优惠码：YE4450，报优惠码，抵两元券，(⊙o⊙)…

转载地址：[http://www.cnblogs.com/legend\_sun/archive/2009/03/20/1417429.html](http://www.cnblogs.com/legend_sun/archive/2009/03/20/1417429.html)