ISNULL (Transact-SQL)
以指定的取代值來取代 NULL。
-
check_expression
-
這是要檢查 NULL 的運算式。check_expression 可以是任何類型。
-
replacement_value
-
這是 check_expression 是 NULL 時所傳回的運算式。replacement_value 必須是能夠隱含地轉換成 check_expresssion 類型的類型。
A. 搭配 AVG 使用 ISNULL
下列範例會尋找所有產品的加權平均值。它會在 Product 資料表的 Weight 資料行中,用值 50 來取代所有 NULL 項目。
USE AdventureWorks; GO SELECT AVG(ISNULL(Weight, 50)) FROM Production.Product; GO
以下為結果集:
-------------------------- 59.79 (1 row(s) affected)
B. 使用 ISNULL
下列範例會選取 AdventureWorks 中所有特殊供應項目的描述、折扣百分比、最小數量和最大數量。如果所有特殊供應項目的最大數量是 NULL,結果集所顯示的 MaxQty 便是 0.00。
USE AdventureWorks; GO SELECT Description, DiscountPct, MinQty, ISNULL(MaxQty, 0.00) AS 'Max Quantity' FROM Sales.SpecialOffer; GO
以下為結果集:
Description DiscountPct MinQty Max Quantity --------------- ------------- -------- --------------- No Discount 0.00 0 0 Volume Discount 0.02 11 14 Volume Discount 0.05 15 4 Volume Discount 0.10 25 0 Volume Discount 0.15 41 0 Volume Discount 0.20 61 0 Mountain-100 Cl 0.35 0 0 Sport Helmet Di 0.10 0 0 Road-650 Overst 0.30 0 0 Mountain Tire S 0.50 0 0 Sport Helmet Di 0.15 0 0 LL Road Frame S 0.35 0 0 Touring-3000 Pr 0.15 0 0 Touring-1000 Pr 0.20 0 0 Half-Price Peda 0.50 0 0 Mountain-500 Si 0.40 0 0 (16 row(s) affected)

