Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:35:50+00:00 2026-05-26T03:35:50+00:00

At any time in the past, if someone had asked me the maximum size

  • 0

At any time in the past, if someone had asked me the maximum size for a varchar(max), I’d have said 2GB, or looked up a more exact figure (2^31-1, or 2147483647).

However, in some recent testing, I discovered that varchar(max) variables can apparently exceed this size:

create table T (
    Val1 varchar(max) not null
)
go
declare @KMsg varchar(max) = REPLICATE('a',1024);
declare @MMsg varchar(max) = REPLICATE(@KMsg,1024);
declare @GMsg varchar(max) = REPLICATE(@MMsg,1024);
declare @GGMMsg varchar(max) = @GMsg + @GMsg + @MMsg;
select LEN(@GGMMsg)
insert into T(Val1) select @GGMMsg
select LEN(Val1) from T

Results:

(no column name)
2148532224
(1 row(s) affected)
Msg 7119, Level 16, State 1, Line 6
Attempting to grow LOB beyond maximum allowed size of 2147483647 bytes.
The statement has been terminated.

(no column name)
(0 row(s) affected)

So, given that I now know that a variable can exceed the 2GB barrier – does anyone know what the actual limit is for a varchar(max) variable?


(Above test completed on SQL Server 2008 (not R2). I’d be interested to know whether it applies to other versions)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:35:50+00:00Added an answer on May 26, 2026 at 3:35 am

    As far as I can tell there is no upper limit in 2008.

    In SQL Server 2005 the code in your question fails on the assignment to the @GGMMsg variable with

    Attempting to grow LOB beyond maximum allowed size of 2,147,483,647
    bytes.

    the code below fails with

    REPLICATE: The length of the result exceeds the length limit (2GB) of
    the target large type.

    However it appears these limitations have quietly been lifted. On 2008

    DECLARE @y VARCHAR(MAX) = REPLICATE(CAST('X' AS VARCHAR(MAX)),92681); 
    
    SET @y = REPLICATE(@y,92681);
    
    SELECT LEN(@y) 
    

    Returns

    8589767761
    

    I ran this on my 32 bit desktop machine so this 8GB string is way in excess of addressable memory

    Running

    select internal_objects_alloc_page_count
    from sys.dm_db_task_space_usage
    WHERE session_id = @@spid
    

    Returned

    internal_objects_alloc_page_co 
    ------------------------------ 
    2144456    
    

    so I presume this all just gets stored in LOB pages in tempdb with no validation on length. The page count growth was all associated with the SET @y = REPLICATE(@y,92681); statement. The initial variable assignment to @y and the LEN calculation did not increase this.

    The reason for mentioning this is because the page count is hugely more than I was expecting. Assuming an 8KB page then this works out at 16.36 GB which is obviously more or less double what would seem to be necessary. I speculate that this is likely due to the inefficiency of the string concatenation operation needing to copy the entire huge string and append a chunk on to the end rather than being able to add to the end of the existing string. Unfortunately at the moment the .WRITE method isn’t supported for varchar(max) variables.

    Paul White confirms the above supposition here and also provides the information that the variable is held entirely in memory if <= 512KB and changes to the tempdb-based backup scheme for values larger than that.

    Addition

    I’ve also tested the behaviour with concatenating nvarchar(max) + nvarchar(max) and nvarchar(max) + varchar(max). Both of these allow the 2GB limit to be exceeded. Trying to then store the results of this in a table then fails however with the error message Attempting to grow LOB beyond maximum allowed size of 2147483647 bytes. again. The script for that is below (may take a long time to run).

    DECLARE @y1 VARCHAR(MAX) = REPLICATE(CAST('X' AS VARCHAR(MAX)),2147483647); 
    SET @y1 = @y1 + @y1;
    SELECT LEN(@y1), DATALENGTH(@y1)  /*4294967294, 4294967292*/
    
    
    DECLARE @y2 NVARCHAR(MAX) = REPLICATE(CAST('X' AS NVARCHAR(MAX)),1073741823); 
    SET @y2 = @y2 + @y2;
    SELECT LEN(@y2), DATALENGTH(@y2)  /*2147483646, 4294967292*/
    
    
    DECLARE @y3 NVARCHAR(MAX) = @y2 + @y1
    SELECT LEN(@y3), DATALENGTH(@y3)   /*6442450940, 12884901880*/
    
    /*This attempt fails*/
    SELECT @y1 y1, @y2 y2, @y3 y3
    INTO Test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any time in which a reference type is more efficient than a
For the past time I have been working on a Zend Framework based webshop
Any time I use the recipe at http://code.activestate.com/recipes/134892/ I can't seem to get it
any time i write this: $this->setTemplate('module/action'); I'm getting this error: The template module/actionSuccess.php does
I need to force any time related operations to GMT/UTC, regardless the timezone set
Can anyone suggest a way to shave any time off this script? var countObject
IE9 has this ridiculous blue arrow that's rendered any time the browser window has
In Windows Forms , you can know, at any time, the current position of
Are boost ptime instances always UTC? I can't see any time zone info on
I'm working in winforms, and I'm attempting to link in some events any time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.