2014-11-04 64 views
0

在下面的查詢中,我選擇當前庫存的產品。現在如果庫存爲null,我想替換爲0。請幫我做這件事。如果在sql服務器中的值爲0,則爲0

SELECT p.ProductID, 
     p.ProductName, 
     (SELECT ISNULL(CurrentStock,0.00) 
     FROM Productstatus PS 
     WHERE PS.ProductID =p.ProductID 
       AND PS.LocationID = 1 
       AND PS.StatusDateTime= '2014-08-27' 
       and PS.productid=p.productid) CurrentStock 
FROM Product P 
LEFT OUTER JOIN LocationProductMap LMP ON LMP.ProductID=P.ProductID 
WHERE LMP.ProductInFlow=1 

回答

2
SELECT p.ProductID, 
    p.ProductName, 
    ISNULL((SELECT ISNULL(CurrentStock,0.00) 
    FROM Productstatus PS 
    WHERE PS.ProductID =p.ProductID 
      AND PS.LocationID = 1 
      AND PS.StatusDateTime= '2014-08-27' 
      and PS.productid=p.productid),0) CurrentStock 
FROM Product P 
LEFT OUTER JOIN LocationProductMap LMP ON LMP.ProductID=P.ProductID 
WHERE LMP.ProductInFlow=1 
0

可以使用

Select case when (CurrentStock) is null then 0 else (CurrentStock) end from table 
0

單COALESCE將做的工作在你的查詢。它比ISNULL功能更有效。

SELECT p.ProductID, 
    p.ProductName, 
    COALESCE((SELECT CurrentStock 
    FROM Productstatus PS 
    WHERE PS.ProductID =p.ProductID 
      AND PS.LocationID = 1 
      AND PS.StatusDateTime= '2014-08-27' 
      and PS.productid=p.productid),0) CurrentStock 
FROM Product P 
LEFT OUTER JOIN LocationProductMap LMP ON LMP.ProductID=P.ProductID 
WHERE LMP.ProductInFlow=1 
相關問題