• I thought the explanation was really lacking something ... like explaining

    The REPLICATE function only returns 8000 characters (defaults to VARCHAR(8000)) if the character value to be replicated isn't explicitly CAST as VARCHAR(MAX). As both REPLICATE statements are internally CAST as VARCHAR(8000) fields, the concatenation of the two VARCHAR(8000) fields yields another VARCHAR(8000) field before assigning the value to the @STR field, hence the results is 8000, not 16000 as you might expect.

    To try and make that clearer ...

    DECLARE @STR VARCHAR(MAX);

    -- The Example from the QOTD

    SET @STR=REPLICATE('*',10000) + REPLICATE('*',10000);

    PRINT LEN(@Str);

    -- Explicitly CAST the Replicated character as type VARCHAR(MAX)

    SET @STR=REPLICATE(CAST('*' AS VARCHAR(MAX)),10000) + REPLICATE(CAST('*' AS VARCHAR(MAX)),10000);

    PRINT LEN(@Str);

    -- Explicitly CAST the results from REPLICATE statements as VARCHAR(MAX), without casting the Replicated character as VARCHAR(MAX)

    SET @STR=CAST(REPLICATE('*',10000) AS VARCHAR(MAX)) + CAST(REPLICATE('*',10000) AS VARCHAR(MAX));

    PRINT LEN(@Str);

    Gives the results:

    8000

    20000

    16000