XCCH004
XCCH004

Reputation: 323

Applying ISNULL inside of a CASE expression

I need to have a way to apply ISNULL in a CASE expression that will replace the NULL values with 0. The below code runs but still returns NULL values. Is there any way to accomplish this? There are two stock types - 'A' and blank, I am trying to get the sum of the quantity for each type.

I've tried using ISNULL inside the CASE

CASE WHEN MRP.stock_type = 'A' 
    THEN ISNULL(SUM(MRP.QUANTITY),0)   
END AS 'Uncovered_Quantity', 
CASE WHEN MRP.stock_type = ' '
    THEN  ISNULL(SUM(MRP.QUANTITY),0)
END AS 'Blank_Quantity',

Upvotes: 0

Views: 43

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

I think you probably intended conditional aggregation:

SUM(CASE WHEN MRP.stock_type = 'A' THEN MRP.QUANTITY ELSE 0 END) as Uncovered_Quantity, 
SUM(CASE WHEN MRP.stock_type = ' ' THEN MRP.QUANTITY ELSE 0 END) as Blank_Quantity,

Upvotes: 2

Related Questions