简体   繁体   English

Null的大小写表达问题

[英]Case expression issue with Null's

I am training on a northwind database but got stuck in a case statement, i am placing the query below and would appreciate assistance. 我正在罗斯文(Northwind)数据库上进行培训,但被困在案例陈述中,我将查询放在下面,希望能为您提供帮助。

The issue is i want the records in column Shippeddate to have some text in this case i placed 'ddd' when there is NULL on it, but NULL's keep appearing in the result set. 问题是我希望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 

try using ISNULL() instead: 尝试使用ISNULL()代替:

so rather than 所以而不是

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

use: 采用:

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

so your query will be like: 因此您的查询将类似于:

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 

Use WHEN ShippedDate IS NULL instead of CASE ShippedDate WHEN NULL : 使用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 
.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM