Probably to make it clear, Its not the len() function that caused the problem. Its operator precedence in "concatenation" operation that caused the problem. In Jacob's example. the datatypes are varchar(5000). So, while concatenating it estimated that result will be varchar(8000). So, it truncated remaining text.
DECLARE @s1 VARCHAR(max) = REPLICATE('a', 8000)
DECLARE @s2 VARCHAR(max)= REPLICATE ('a', 8000)
DECLARE @s3 VARCHAR(max)= REPLICATE ('a', 2000)
select LEN(@s1+@s2+@s3)
Above example works fine. Here the datatypes are varchar(max). So, the result also it estimates that it will be varchar(max) and will correctly do the work.
commented on May 31 2011 6:44AM