Taken from the docs:
If the result of the concatenation of strings exceeds the limit of
8,000 bytes, the result is truncated. However, if at least one of the
strings concatenated is a large value type, truncation does not occur.
Operations between varchar and nvarchar are limited to 8000 and 4000 characters respectively, unless you treat any of the involved data types as MAX
. Please be very cautious with the order of the operations, this is a very good example from the docs:
DECLARE @x varchar(8000) = replicate('x', 8000)
DECLARE @y varchar(max) = replicate('y', 8000)
DECLARE @z varchar(8000) = replicate('z',8000)
SET @y = @x + @z + @y
-- The result of following select is 16000
SELECT len(@y) AS y
The result is 16k and not 24k because the first operation is @x + @z
which is truncated at 8000 because neither of them are MAX
. Then the result is concatenated to a type that is MAX
, thus breaking the restriction of 8000 as limit, which adds another 8000 characters from @y
. In the result, the characters from variable @z
are lost at the first concatenation.