title: SQL: Using ISNULL with dynamic pivot
body: <p>I want to make all the NULL values produced by the pivot to become 0s.  I have placed ISNULL in every place imaginable, but does not seem to have any effect.  Are pivots compatible with ISNULL?  Code below:       </p>
  
  <pre><code>    DECLARE @startDate datetime
      SET @startDate = '2013-01-01'
  
      DECLARE @sql varchar(MAX)
      SET @sql = 'SELECT 
      CLIENTNAME, PROJECTNAME, RESOURCE, [' +
        REPLACE(SUBSTRING(CONVERT(varchar, @startDate, 13), 4, 8), ' ', '') + '], [' +
        REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 1, @startDate), 13), 4, 8), ' ', '') + '], [' +
        REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 2, @startDate), 13), 4, 8), ' ', '') + '], [' +
        REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 3, @startDate), 13), 4, 8), ' ', '') + '], [' +
        REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 4, @startDate), 13), 4, 8), ' ', '') + '], [' +
        REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 5, @startDate), 13), 4, 8), ' ', '') + ']
      FROM
        (
        SELECT
          CLIENTNAME, PROJECTNAME, RESOURCE, FORECASTTOTAL
        FROM viewprojscheduling_group
        ) AS SourceTable
      PIVOT
        (
        SUM(FORECASTTOTAL)
        FOR SCHEDULEDDATE IN (' +
                QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, @startDate, 13), 4, 8), ' ', '')) + ', ' +
                QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 1, @startDate), 13), 4, 8), ' ', '')) + ', ' +
                QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 2, @startDate), 13), 4, 8), ' ', '')) + ', ' +
                QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 3, @startDate), 13), 4, 8), ' ', '')) + ', ' +
                QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 4, @startDate), 13), 4, 8), ' ', '')) + ', ' +
                QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 5, @startDate), 13), 4, 8), ' ', '')) + ')
        ) AS PivotTable'
  
      execute(@sql)
  </code></pre>