您当前的位置: 首页 > 数据库教程 > MySQL教程 > sql判断null Oracle,sql server

sql判断null Oracle,sql server

作者:不详 来源:网络 发布时间: 2014-07-17 09:34 点击:
sql server 替换null:isnull(arg,value) 如:select isnull(price,0.0) from orders ,如果price为null的话,用0.0替换 与null比较: is not null,is null 如 select * from orders where price is null ,price等于null 如: select * from orders where price is not nul

sql判断null Oracle,sql server

  sql server

  替换null:isnull(arg,value)

  如:select isnull(price,0.0) from orders ,如果price为null的话,用0.0替换

  与null比较: is not null,is null

  如 select * from orders where price is null ,price等于null

  如: select * from orders where price is not null ,price不等于null

  oracle

  替换null: nvl(arg,value)

  如: select nvl(price,0.0) form orders

  与null比较: is not null,is null

  如 select * from orders where price is null ,price等于null

  如: select * from orders where price is not null ,price不等于null

  oracle

  这是一个经常被问到的问题。尤其是客户之前使用的是oracle,那么他在使用sql server的时候会有一个疑问,就是在处理null值上面,sql server与oracle的行为不一样

  在oracle中,null值会认为是一个无穷大的值,所以如果按照升序排列的话,则会被排在最后面

  在sql server中则正好相反,null值会被认为是一个无穷小的值,所以如果按照升序排列的话,则会被排在最前面

  如

  select [id]

  from [demo].[dbo].[orders] order by id

  则会看到如下的效果

  那么,有没有什么办法让sql server的这个默认机制与oracle一样吗?答案是:没有

  但我们可以想一些变通的办法,例如可以像下面这样写代码

  select [id]

  from [demo].[dbo].[orders] order by case when id is null then 1 else 0 end

  这样的话,就可以看到如下的效果

  如果该列有创建索引,那么可以看到如下的执行计划
分享到:
本文"sql判断null Oracle,sql server"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4