While creating some CLR user defined functions I discovered that using data passed into my nvarchar(max) parameter was getting truncated if it was more than 8000 bytes.
I was creating the LOBs using the REPLICATE function. My last resort was to make sure I really was passing in more than 8000 bytes which lead me to read the documentation for REPLICATE.
If string_expression is not of type varchar(max) or nvarchar(max), REPLICATE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, string_expression must be explicitly cast to the appropriate large-value data type.
There was my problem.
DECLARE @result nvarchar(max)
SET @string = N'A'
SET @result = REPLICATE(N'A',4001)
SET @result = REPLICATE(CAST(N'A' as nvarchar(max)),4001)
SET @result = REPLICATE(@string,4001)
This is the link to the request for assistance on SQL Server Central if you are interested in the details of the LOB parameters to CLR functions.