views:

317

answers:

3

Guys,

In SSMS when I try to execute:

SELECT CONVERT(VARCHAR(MAX), REPLICATE('a',9000))

I see only the first 8000 characters displayed. The settings Tool >> Options >> Query Results >> Sql Server >> Results to Grid is set to 65534 and Results to Text is set to 8192.

Also when I try to run this from SQLCMD

sqlcmd -S Server -E -y 0 -Q "SELECT CONVERT(VARCHAR(MAX), REPLICATE('a',9000))" -o out.txt

I see only 8000 charecters.

The flag -y 0 is supposed to set it up to 1 MB. But I do not more than 8000 characters.

What could be the problem?

thanks,
_UB

A: 

VARCHAR(MAX) is only capable of holding 8000 characters

see here

From MSDN:

varchar [ ( n | max ) ] - Variable-length, non-Unicode character data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes

As Joseph (below) says, to hold more, use a text or ntext data type, but if you want to be able to search that, then you'd need some form of full-text indexing enabled.

Better Link

MasterMax1313
varchar(max) is a LOB type! Your quote actually says "max indicates that the maximum storage size is 2^31-1 bytes"
gbn
my appologies on the poorlink, see here http://doc.ddart.net/mssql/sql70/da-db_1.htm for a full layout. Also, that's storage size, not max characters.
MasterMax1313
Even worse: the new link is for SQL 7 which did not have varchar(max) which *replaces text datatype*
gbn
really ? I remember storing more than 8000 characters in VARCHAR(MAX) before. As far as I know, it can store up to 2 GB data.
UB
From BoL:max indicates that the maximum storage size is 2^31-1 bytes
UB
+1  A: 

See here :

http://www.sqlservercentral.com/articles/varchar%28max%29/67057/

CodeByMoonlight
Thanks for the response. I read that article before...
UB
+1  A: 

REPLICATE output is based on the datatype input. So this explains sqlcmd.

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.

So, use this SELECT REPLICATE(CONVERT(VARCHAR(MAX), 'a'), 9000)

And SSMS has never shown all text data (nor did Query Analyzer)

gbn
ahh... REPLICATE. I knew that. Just did not think of it before posting. My bad.Thanks for the response.
UB