Null 的大小写表达式问题

Case expression issue with Null's

我正在使用 northwind 数据库进行训练,但卡在了 case 语句中,我将查询放在下面,希望得到帮助。

问题是我希望 Shippeddate 列中的记录有一些文本,在这种情况下,当上面有 NULL 时我放置了 'ddd',但是 NULL 一直出现在结果集中。

SELECT OrderID, 
       CONVERT(VARCHAR(10), OrderDate, 103)    AS OrderDate, 
       CONVERT(VARCHAR(10), RequiredDate, 103) AS RequiredDate, 
       CASE ShippedDate 
         WHEN NULL THEN 'ddd' 
         ELSE CONVERT(VARCHAR(10), ShippedDate, 103) 
       END                                     AS ShippedDate 
       -- Why is this not working 
       , 
       [UnitPrice]                             AS UnitPriceOnOrder, 
       [Quantity]                              AS QuantityOnOrder, 
       [Discount]                              AS DiscountOnOrder, 
       CompanyName_II                          AS CustomerCompanyName, 
       ContactName_II                          AS CustomerContact, 
       ContactTitle_II                         AS CustomerContactTitle, 
       City_II                                 AS CustomerCity, 
       Country_II                              AS CustomerCountry, 
       ProductName, 
       CategoryName, 
       [Description]                           AS CategoryDescription, 
       UnitsInStock, 
       UnitsOnOrder, 
       UnitsInStock - UnitsOnOrder             AS AvailableUnitsInStock, 
       FirstName + ' ' + LastName              AS EmployeeName 
FROM   ##NorthwindTestI 
WHERE  [Quantity] > 10 
       AND ShippedDate > '19970101' 
        OR [UnitPrice] > 10 
        OR Country_II = 'USA' 
        OR FirstName + ' ' + LastName = 'Janet Leverling' 
ORDER  BY 
--[Quantity] 
ShippedDate 

使用WHEN ShippedDate IS NULL代替CASE ShippedDate WHEN NULL:

SELECT .....,
       CASE WHEN ShippedDate IS NULL  
        THEN 'ddd' 
        ELSE CONVERT(VARCHAR(10), ShippedDate, 103) 
       END  AS ShippedDate 
.....

尝试使用 ISNULL() 代替:

所以而不是

CASE ShippedDate 
         WHEN NULL THEN 'ddd' 
         ELSE CONVERT(VARCHAR(10), ShippedDate, 103) 
       END                                     AS ShippedDate 

使用:

ISNULL(CONVERT(VARCHAR(10),ShippedDate,103),'ddd')

因此您的查询将类似于:

SELECT OrderID, 
       CONVERT(VARCHAR(10), OrderDate, 103)    AS OrderDate, 
       CONVERT(VARCHAR(10), RequiredDate, 103) AS RequiredDate, 
       ISNULL(CONVERT(VARCHAR(10),ShippedDate,103),'ddd') AS ShippedDate 
       -- Why is this not working 
       , 
       [UnitPrice]                             AS UnitPriceOnOrder, 
       [Quantity]                              AS QuantityOnOrder, 
       [Discount]                              AS DiscountOnOrder, 
       CompanyName_II                          AS CustomerCompanyName, 
       ContactName_II                          AS CustomerContact, 
       ContactTitle_II                         AS CustomerContactTitle, 
       City_II                                 AS CustomerCity, 
       Country_II                              AS CustomerCountry, 
       ProductName, 
       CategoryName, 
       [Description]                           AS CategoryDescription, 
       UnitsInStock, 
       UnitsOnOrder, 
       UnitsInStock - UnitsOnOrder             AS AvailableUnitsInStock, 
       FirstName + ' ' + LastName              AS EmployeeName 
FROM   ##NorthwindTestI 
WHERE  [Quantity] > 10 
       AND ShippedDate > '19970101' 
        OR [UnitPrice] > 10 
        OR Country_II = 'USA' 
        OR FirstName + ' ' + LastName = 'Janet Leverling' 
ORDER  BY 
--[Quantity] 
ShippedDate