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 8592315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:43:27+00:00 2026-06-11T23:43:27+00:00

All, I have a large (unavoidable) dynamic SQL query. Due to the number of

  • 0

All, I have a large (unavoidable) dynamic SQL query. Due to the number of fields in the selection criteria the string containing the dynamic SQL is growing over 4000 chars. Now, I understand that there is a 4000 max set for NVARCHAR(MAX), but looking at the executed SQL in Server Profiler for the statement

DELARE @SQL NVARCHAR(MAX);
SET @SQL = 'SomeMassiveString > 4000 chars...';
EXEC(@SQL);
GO

Seems to work(!?), for another query that is also large it throws an error which is associated with this 4000 limit(!?), it basically trims all of the SQL after this 4000 limit and leaves me with a syntax error. Despite this in the profiler, it is showing this dynamic SQL query in full(!?).

What exactly is happening here and should I just be converting this @SQL variable to VARCHAR and get on with it?

Thanks for your time.

Ps. It would also be nice to be able to print out more than 4000 chars to look at these big queries. The following are limited to 4000

SELECT CONVERT(XML, @SQL);
PRINT(@SQL);

is there any other cool way?

  • 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-06-11T23:43:28+00:00Added an answer on June 11, 2026 at 11:43 pm

    I understand that there is a 4000 max set for NVARCHAR(MAX)

    Your understanding is wrong. nvarchar(max) can store up to (and beyond sometimes) 2GB of data (1 billion double byte characters).

    From nchar and nvarchar in Books online the grammar is

    nvarchar [ ( n | max ) ]
    

    The | character means these are alternatives. i.e. you specify either n or the literal max.

    If you choose to specify a specific n then this must be between 1 and 4,000 but using max defines it as a large object datatype (replacement for ntext which is deprecated).

    In fact in SQL Server 2008 it seems that for a variable the 2GB limit can be exceeded indefinitely subject to sufficient space in tempdb (Shown here)

    Regarding the other parts of your question

    Truncation when concatenating depends on datatype.

    1. varchar(n) + varchar(n) will truncate at 8,000 characters.
    2. nvarchar(n) + nvarchar(n) will truncate at 4,000 characters.
    3. varchar(n) + nvarchar(n) will truncate at 4,000 characters. nvarchar has higher precedence so the result is nvarchar(4,000)
    4. [n]varchar(max) + [n]varchar(max) won’t truncate (for < 2GB).
    5. varchar(max) + varchar(n) won’t truncate (for < 2GB) and the result will be typed as varchar(max).
    6. varchar(max) + nvarchar(n) won’t truncate (for < 2GB) and the result will be typed as nvarchar(max).
    7. nvarchar(max) + varchar(n) will first convert the varchar(n) input to nvarchar(n) and then do the concatenation. If the length of the varchar(n) string is greater than 4,000 characters the cast will be to nvarchar(4000) and truncation will occur.

    Datatypes of string literals

    If you use the N prefix and the string is <= 4,000 characters long it will be typed as nvarchar(n) where n is the length of the string. So N'Foo' will be treated as nvarchar(3) for example. If the string is longer than 4,000 characters it will be treated as nvarchar(max)

    If you don’t use the N prefix and the string is <= 8,000 characters long it will be typed as varchar(n) where n is the length of the string. If longer as varchar(max)

    For both of the above if the length of the string is zero then n is set to 1.

    Newer syntax elements.

    1. The CONCAT function doesn’t help here

    DECLARE @A5000 VARCHAR(5000) = REPLICATE('A',5000);
    
    SELECT DATALENGTH(@A5000 + @A5000), 
           DATALENGTH(CONCAT(@A5000,@A5000));
    

    The above returns 8000 for both methods of concatenation.

    2. Be careful with +=

    DECLARE @A VARCHAR(MAX) = '';
    
    SET @A+= REPLICATE('A',5000) + REPLICATE('A',5000)
    
    DECLARE @B VARCHAR(MAX) = '';
    
    SET @B = @B + REPLICATE('A',5000) + REPLICATE('A',5000)
    
    
    SELECT DATALENGTH(@A), 
           DATALENGTH(@B);`
    

    Returns

    -------------------- --------------------
    8000                 10000
    

    Note that @A encountered truncation.

    How to resolve the problem you are experiencing.

    You are getting truncation either because you are concatenating two non max datatypes together or because you are concatenating a varchar(4001 - 8000) string to an nvarchar typed string (even nvarchar(max)).

    To avoid the second issue simply make sure that all string literals (or at least those with lengths in the 4001 – 8000 range) are prefaced with N.

    To avoid the first issue change the assignment from

    DECLARE @SQL NVARCHAR(MAX);
    SET @SQL = 'Foo' + 'Bar' + ...;
    

    To

    DECLARE @SQL NVARCHAR(MAX) = ''; 
    SET @SQL = @SQL + N'Foo' + N'Bar'
    

    so that an NVARCHAR(MAX) is involved in the concatenation from the beginning (as the result of each concatenation will also be NVARCHAR(MAX) this will propagate)

    Avoiding truncation when viewing

    Make sure you have "results to grid" mode selected then you can use

    select @SQL as [processing-instruction(x)] FOR XML PATH 
    

    The SSMS options allow you to set unlimited length for XML results. The processing-instruction bit avoids issues with characters such as < showing up as &lt;.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a large number of programmers on different platforms all using CVS. We
Lets say I have a large number of worker threads all actively processing, and
I have a large number of different NSObject types that all have different properties
Hey all I have a large html string like <a style=background: rgb(100, 101, 43)
I have large string which I split by newlines. How can I remove all
I have a large list [[1,.., ..],[2,...,...],[5,...,...],[1,...,...]] I need to remove all elements that
On large-scale Java/.Net Enterprise projects, does every developer need to have all the components/libraries/dependencies
I have a relatively large table (5,208,387 rows, 400mb data/670mb index), all columns i
We have a scheduling engine with large amounts of test data to test all
We have a large amount of apps. They all have a build.xml file located

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.