I am training on a northwind database but got stuck in a case statement, i am placing the query below and would appreciate assistance.
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.
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:
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
:
SELECT .....,
CASE WHEN ShippedDate IS NULL
THEN 'ddd'
ELSE CONVERT(VARCHAR(10), ShippedDate, 103)
END AS ShippedDate
.....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.