Question : SQL Pivot : Setting Nulls to 0

I have the following statement:


DECLARE @listCol VARCHAR(2000)
SELECT  @listCol = STUFF((SELECT DISTINCT
'],[' + Description
FROM    Items
ORDER BY '],[' + Description FOR XML PATH('')), 1, 2, '') + ']'
DECLARE @query NVARCHAR(4000)
SET @query = 'SELECT * FROM assets
PIVOT( SUM(AssetQTY) FOR AssetType IN ('+@listCol+')) AS p'
EXECUTE(@query)

This returns some nulls depending on the data and I would like to have these set to 0. Any ideas? I am using SQL Server 2008.

Cheers

Answer : SQL Pivot : Setting Nulls to 0

did you remove the l from the @selcoll variable in the select?

DECLARE @listCol VARCHAR(max),@selcol varchar(max),@sql varchar(max)

SELECT  @listCol = coalesce(@listcol+',','')+'['+description+']'
       ,@selcol=coalesce(@selcol+',','')+'coalesce(['+description+'],0) as ['
    +description+']'
from (SELECT top 100 percent DISTINCT Description
FROM    Items
ORDER BY Description) as x


SET @sql = 'SELECT '+@selcol+' FROM assets '
         + 'PIVOT( SUM(AssetQTY) FOR AssetType IN ('+@listCol+')) AS p'
print @sql
EXECUTE(@sql)


please post the constructed statement...

what is/was the problem, this syntax has worked for me in the past.
Random Solutions  
 
programming4us programming4us