Domanda

I have been through many similar questions but no desired result.

I have two tables:

Table Listings:

SKU(int)    EAN(int)

1           923983737483
2           998379847983
3           978979879080

Table Stock

SKU(int)            qtyInHand(int)

1                   4
2                   NULL
3                   2

I want to get the list of SKUs, EAN and qtyInHand based on the following conditions

Example:

Select 
    Listings.SKU, Listings.EAN, 
    IF (Stock.qtyInHand is null or stock.qtyInHand < 2) then 0 AS Stock

I have used the following query but it still list null values under stock field.

SELECT 
   CASE 
      WHEN (dbo.Stock.qtyInHand IS NULL) OR (dbo.Stock.qtyInHand < 2) 
      THEN 0 
   END AS Stock, 
   dbo.Listings.SKU, dbo.Listings.EAN
FROM 
   dbo.Listings 
LEFT OUTER JOIN 
   dbo.Stock ON dbo.Listings.SKU = dbo.Stock.SKU
È stato utile?

Soluzione

Try this, Isnull() Function

SELECT 
    CASE WHEN (Isnull(dbo.Stock.qtyInHand,0) < 2) 
        THEN 0 
        Else dbo.Stock.qtyInHand
        END AS Stock
        , dbo.Listings.SKU
        , dbo.Listings.ASIN 
    FROM dbo.Listings 
LEFT OUTER JOIN dbo.Stock ON dbo.Listings.SKU = dbo.Stock.SKU

Altri suggerimenti

Your CASE statement does not include an ELSE to cover the situation where qtyInHand is not NULL or is greater than or equal to 2. As stated in MSDN, the CASE statement evaluates to NULL whenever none of the WHEN expressions are true.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top