码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver、oracle数据库排序空值null问题解决办法

时间:2018-09-11 11:23:35      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:解决   lse   span   arc   log   rac   sele   case when   .com   

转:https://www.cnblogs.com/pacer/archive/2010/03/02/1676371.html

【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 

【oracle】:

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、oracle数据库排序空值null问题解决办法

标签:解决   lse   span   arc   log   rac   sele   case when   .com   

原文地址:https://www.cnblogs.com/linvan/p/9625530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!