• I've aware that when you concatenate strings, the data type of the entire concatenated string will be set according to the implicit data type of the first item, which can cause the kind of problem you had. So if the first item is an ordinary string, the whole concatenated string will be chopped down to the maximum length of 4000 for unicode or 8000 for non-unicode. Maybe making the first item an explicitly NVARCHAR(MAX) data type will help, like this:

    SET @DynamicPivotQuery = CONVERT(NVARCHAR(MAX),N'') +N'.....your concatenated stuff here...