SQL語句IsNull函式應用方法

2021-09-27 05:53:37 字數 557 閱讀 4472

p_id

productname

unitprice

unitsinstock

unitsonorder

1computer

69925152

printer

36536

3telephone

280159

57假如 "unitsonorder" 是可選的,而且可以包含 null 值。

上面是微軟中的例子,如果要計算unitsonorder中的值,因為有null值,是無法計算的,所以可以用isnull來做個判斷,如果是空的null值,那就返回乙個0,如果不是空的,就返回當前值。這樣在計算時,就不會出現sql語句錯誤。

select productname,unitprice*(unitsinstock+isnull(unitsonorder,0))

from products

在這裡unitprice單價x(unitsinstock庫存量+unitsonorder訂單量),可以得出總價。如果unitsonorder是空的,那麼就是0,也就是沒有訂單,這樣計算就不會錯誤了。

SQL中ISNULL函式使用介紹

isnull value1,value2 如果在select中就是isnull可以判斷是否是null,如果是給個預設值,isnull 欄位名 設定預設的資料 例如 select isnull 分數,0 from xuesheng where name 張三 在表xuesheng中,字段分數如果為空,...

Isnull 函式測試

usetest go isnull 函式測試 2009 11 22 isnull check expression replacement value 1。資料型別一樣,但長度不一樣 declare xnvarchar 5 y nvarchar 50 set y n 123456 print isn...

Isnull 函式測試

usetest go isnull 函式測試 2009 11 22 isnull check expression replacement value 1。資料型別一樣,但長度不一樣 declare xnvarchar 5 y nvarchar 50 set y n 123456 print isn...