1

In the query below nvarchar(max) seems to truncate at 4000 characters. That seems contrary to the answer here which says "Since NVARCHAR uses 2 bytes per character, that's approx. 1 billion characters."

DECLARE @test NVARCHAR(MAX)
DECLARE @i INT
SET @i=0
set @test=cast('x' as nvarchar(max))

while(@i<6000)
begin
set @test= (@test+ cast('x' as nvarchar(max)))
set @i=@i+1

end

print @TEST--has only 4000 characters
Community
  • 1
  • 1
Foo
  • 4,206
  • 10
  • 39
  • 54

1 Answers1

1

Sorry, just found out that this is a limitation of the message screen in Sql server management studio. The code below shows correct results

DECLARE @test NVARCHAR(MAX)
DECLARE @i INT
SET @i=0
set @test=cast('x' as nvarchar(max))

while(@i<6000)
begin
set @test= (@test+ cast('x' as nvarchar(max)))
set @i=@i+1

end

print len(@TEST)--6001
Foo
  • 4,206
  • 10
  • 39
  • 54
  • 1
    Wow, yes, that's *bizarre*! Using `print` with an nvarchar(max) will only print the first 4000 characters! Great find. Hey Microsoft: How about you fix your shit? :) – NathanAldenSr Mar 19 '14 at 21:01