views:

860

answers:

2

How many characters can a SQL Server 2008 database field contain when the data type is VARCHAR(MAX)?

Thanks guys, helped me out a lot :)

+4  A: 

From http://msdn.microsoft.com/en-us/library/ms176089.aspx

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. The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. The ISO synonyms for varchar are char varying or character varying.

1 character = 1 byte. And don't forget 2 bytes for the termination. So, 2^31-3 characters.

jeffamaphone
Technically, 1 character = 1 byte only for certain character encodings.
Amber
Ah true. DBCS strings, e.g., would give you less. So, watch out for that.
jeffamaphone
@Dav, yeah that was one of my considerations too. However due to the number of bytes available, this will meet my needs for this scenario.
Russell
+2  A: 

See the MSDN reference table for maximum numbers/sizes.

Bytes per varchar(max), varbinary(max), xml, text, or image column: 2^31-1

There's a two-byte overhead for the column, so the actual data is 2^31-3 max bytes in length. Assuming you're using a single-byte character encoding, that's 2^31-3 characters total. (If you're using a character encoding that uses more than one byte per character, divide by the total number of bytes per character. If you're using a variable-length character encoding, all bets are off.)

Amber