Home Forums SQL Server 2005 T-SQL (SS2K5) Replacing NULL with 0 when using the Pivot operator RE: Replacing NULL with 0 when using the Pivot operator

  • Maybe you could try a CROSS-TABS approach.

    SELECT location_name,

    MAX( CASE WHEN month = '2008-03' THEN total_offices ELSE 0 END) [2008-03],

    MAX( CASE WHEN month = '2008-04' THEN total_offices ELSE 0 END) [2008-04]

    FROM #office_info_by_location_id

    GROUP BY location_name

    More on this method and the possibility to make it dynamic on the following links:

    http://www.sqlservercentral.com/articles/T-SQL/63681/

    http://www.sqlservercentral.com/articles/Crosstab/65048/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2