Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace (null) values with 0 output in PIVOT

I tried to convert the (null) values with 0 (zeros) output in PIVOT function but have no success.

Below is the table and the syntax I've tried:

SELECT CLASS, [AZ], [CA], [TX] FROM #TEMP PIVOT (SUM(DATA) FOR STATE IN ([AZ], [CA], [TX])) AS PVT ORDER BY CLASS  CLASS   AZ  CA      TX RICE    10  4       (null) COIN    30  3        2 VEGIE   (null) (null) 9 

I tried to use the ISNULL but did not work.

PIVOT SUM(ISNULL(DATA,0)) AS QTY 

What syntax do I need to use?

like image 769
joe Avatar asked Sep 07 '12 16:09

joe


People also ask

How do I change a pivot table to NULL to zero?

You can use isnull or coalesce to substitute a 0 for null values.

How do I show zero values in a pivot table?

To do that just right click and select “PivotTable Options” under “Format for empty cells” put a “0”and then just click OK. And now you can see that the empty cells are showing a “0”.

What to replace NULL values with?

Null values are replaced with mean/median.


1 Answers

SELECT CLASS, isnull([AZ],0), isnull([CA],0), isnull([TX],0) FROM #TEMP PIVOT (SUM(DATA) FOR STATE IN ([AZ], [CA], [TX])) AS PVT ORDER BY CLASS 
like image 101
Sankara Avatar answered Sep 20 '22 12:09

Sankara