您现在的位置: 万盛学电脑网 >> 程序编程 >> 数据库 >> mysql教程 >> 正文

sql判断null Oracle,sql server

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   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

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

  如果该列有创建索引,那么可以看到如下的执行计划