Wednesday, March 28, 2012

MAX doesn't apply for concatenated variables?

If I load up a local variable defined as varchar(MAX) with a 10,000-character continuous string, i.e. '123456789.....100000' the variable retains the full value as expected. However, if I break that string up into '123456789....5000' + '5001...10000', the variable is truncated at 8000 characters.

Is there a way around this?

Thanks.

This has to do with type conversion. SQL Server does not do an auto coversion from a not max varchar to a max varchar. The key is to cast your values to a varchar(max) type. For example:

select len (replicate ('*',10000))

--

8000

select len (replicate (cast('*' as varchar(max)),10000))

--

10000

sql

No comments:

Post a Comment