求助:关于SQL Server与Oracle对于空字符排序不同处理的咨询

lovelabor 2009-12-22 10:09:57
偶然间发现在字段为字符型参数时,Sql server与Oracle对于空字符排序不一致。

范例如下:
字段A有多个值 空值(null或'')、123、456时,使用order by排序:
1)Sql server排序顺序依次为null或''、123、456
2)Oracle排序顺序依次为123、456、null或''

估计Oracle应该有开关解决这类排序问题,请各位大侠帮忙看看,谢谢。
...全文
160 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
h396463735 2011-05-06
  • 打赏
  • 举报
回复
又是老帖子,但很受教,谢谢6楼。问题解决了。
LiSucceed 2009-12-24
  • 打赏
  • 举报
回复
学习了!
wh62592855 2009-12-22
  • 打赏
  • 举报
回复
1、ORDER BY 中关于NULL的处理

缺省处理,Oracle在Order by 时认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前。

当然,你也可以使用nulls first 或者nulls last 语法来控制NULL的位置。
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



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wh62592855/archive/2009/11/15/4813404.aspx
lovelabor 2009-12-22
  • 打赏
  • 举报
回复
呵呵,本来想看看Oracle有没得开关设置的,这样比较方便一些,对于编程习惯也不会有影响。

感谢各位热心的大侠的支持。

hjb719 2009-12-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wildwave 的回复:]
oracle
order by a nulls first

即可与sqlserver排序一致
[/Quote]
学习了,经验证果然是这样
lovelrforever 2009-12-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dawugui 的回复:]
你可以指定.

在sql server和oracle中都可以这么写.

order by case when id <> '' and id is not null then 1 else 2 end

或者

order by case when id <> '' and id is not null then 2 else 1 end

[/Quote]
在oracle中 ''或null不能用<>来比较
dawugui 2009-12-22
  • 打赏
  • 举报
回复
你可以指定.

在sql server和oracle中都可以这么写.

order by case when id <> '' and id is not null then 1 else 2 end

或者

order by case when id <> '' and id is not null then 2 else 1 end
小灰狼W 2009-12-22
  • 打赏
  • 举报
回复
oracle
order by a nulls first

即可与sqlserver排序一致

3,494

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧