Skip to content
Advertisement

PIVOT function returning NULL values and ISNULL not removing

I have a query using PIVOT function that has NULL values that wish to replace with 0 in the cell. I have tried to add the ISNULL into the sub query against O7CRAM but that did not remove the NULL values

The issue I have now is the ISNULL and where to place it to remove the NULL values.

Current Output:

O7ORNO PNP DTY HAN
100000329 0.85 NULL NULL

Desired Output:

O7ORNO PNP DTY HAN
100000329 0.85 0.00 0.00

Advertisement

Answer

Here’s a way to do the same thing with the ancient, arcane, but simple “Black Arts” method known as a CROSSTAB. Notice how you do NOT neet explicit ISNULL()s.

Here’s a slightly different take with row/column totals and a grand total.

For more information about how CROSSTABs work and for how to make Dynamic CROSSTABS, please see the articles at the following links:

https://www.sqlservercentral.com/articles/cross-tabs-and-pivots-part-1-converting-rows-to-columns-1

https://www.sqlservercentral.com/articles/cross-tabs-and-pivots-part-2-dynamic-cross-tabs

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement