remove NULL from PIVOT table columns

  • How can I remove NULL from SQL-Server PIVOT table dynamic columns.

  • Hello,

    While creating dynamic pivot table, in the code part where column names are generated, you can use a where criteria preventing null values to be displayed as column header.

    If you check the dynamic sql pivot table example, you can add a similar where clause where column headers are concatenated

    DECLARE @PivotColumnHeaders VARCHAR(MAX)

    SELECT @PivotColumnHeaders =

    COALESCE(

    @PivotColumnHeaders + ',[' + cast(Category as varchar) + ']',

    '[' + cast(Category as varchar)+ ']'

    )

    FROM (

    SELECT Distinct Category FROM UserData where value is not null

    ) u

    SELECT @PivotColumnHeaders

    Here the "value" column is the aggregated column in the pivot

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply