Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server STUFF - is it supposed to work with position > 8000?

Compare these two statements

select stuff(convert(varchar(max),replicate('a', 10000)),8001,1,'b')
select stuff(convert(varchar(max),replicate('a', 10000)),8000,1,'b')

Output

aaaaaaaaaaaaaaaaaaaaaaaa...
NULL

Books Online says start can be of type bigint. Why such a large range if it won't even work for 8001?

If the behaviour is different between 2005, 2008, 2008 R2 and Denali, then I would like to know the actual behaviour of each version.

like image 302
RichardTheKiwi Avatar asked Jan 25 '11 00:01

RichardTheKiwi


1 Answers

REPLICATE ('a', 10000) will produce a 8000 characters string:

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.

Try REPLICATE (cast('a' as varchar(max)), 10000).

like image 185
Remus Rusanu Avatar answered Sep 20 '22 14:09

Remus Rusanu