how to store more than 8000 characters in sql server – SQLSERVERLEARNER https://sqlserverlearner.com LEARN SQL SERVER ONLINE Thu, 07 Jun 2012 07:45:02 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 SQL Server Misconception – VARCHAR(MAX) Cannot Store More Than 8000 Characters https://sqlserverlearner.com/2012/06/07/sql-server-misconception-varcharmax-cannot-store-more-than-8000-characters/ https://sqlserverlearner.com/2012/06/07/sql-server-misconception-varcharmax-cannot-store-more-than-8000-characters/#respond Thu, 07 Jun 2012 07:45:02 +0000 https://sqlserverlearner.azurewebsites.net/2012/06/07/sql-server-misconception-varcharmax-cannot-store-more-than-8000-characters/ This post deals with one of the SQL Server Misconception – VARCHAR(MAX) Cannot Store More Than 8000 Characters.

Lets analyse this first
Take the below query.
[sql]
DECLARE @String VARCHAR(MAX)
DECLARE @i INT
SELECT @i = 10000,@String=”

WHILE @i>0
BEGIN
SELECT @String = @String + ‘A’
SET @i = @i – 1
END

PRINT @String
[/sql]

When you run this query you will get the following output.
Output:

Print Output Length

Find the length of the output:
When you move the cursor to the end of the output in the Messages window you can notice that the column number (as shown in above image) to be 8001(So length is 8000 characters).

This leads to the misconception
– VARCHAR(MAX) Cannot Store More Than 8000 Characters

Actuality:
– Print statement has a limit of 8000 characters(non-unicode) hence the output is truncated.
– The varchar(max) statement can store up to 2gb of data(2^31-1 bytes).

Take the below SQL Code
[sql]
DECLARE @String nVARCHAR(MAX)
DECLARE @i INT
SELECT @i = 10000,@String=”

WHILE @i>0
BEGIN
SELECT @String = @String + ‘A’
SET @i = @i – 1
END

SELECT LEN(@String) as Length
[/sql]

Output:
Length
——————–
10000


(1 row(s) affected)

LEN statement returns 10000 and it proves that varchar(max) can store more than 8000 characters.

]]>
https://sqlserverlearner.com/2012/06/07/sql-server-misconception-varcharmax-cannot-store-more-than-8000-characters/feed/ 0