sql判斷null Oracle,sql server

來源:互聯網
上載者:User

   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

  這樣的話,就可以看到如下的效果

  如果該列有建立索引,那麼可以看到如下的執行計畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.