5

I working with SQL Server data base in order to store very long Unicode string. The field is from type 'ntext', which theoretically should be limit to 2^30 Unicode characters.

From MSDN documentation:

ntext

Variable-length Unicode data with a maximum string length of 2^30 - 1 (1,073,741,823) bytes. Storage size, in bytes, is two times the string length that is entered. The ISO synonym for ntext is national text.

I'm made this test:

  1. Generate 50,000 characters string.

  2. Run an Update SQL statement

    UPDATE [table] SET Response='... 50,000 character string...' WHERE ID='593BCBC0-EC1E-4850-93B0-3A9A9EB83123'

  3. Check the result - what actually stored in the field at the end.

The result was that the field [Response] contain only 43,679 characters. All the characters at the end of the string was thrown out.

Why this happens? How I can fix this?

If this is really the capacity limit of this data type (ntext), which another data type can store longer Unicode string?

No1Lives4Ever
  • 6,430
  • 19
  • 77
  • 140

4 Answers4

3

NTEXT datatype is deprecated and you should use NVARCHAR(MAX).

I see two possible explanations:

  1. Your ODBC driver you use to connect to database truncate parameter value when it is too long (try using SSMS)

  2. You write you generate your input string. I suspect you generate CHAR(0) which is Null literal

If second is your case make sure you cannot generate \0 char.

EDIT:

I don't know how you check the length but keep in mind that LEN does not count trailing whitespaces

SELECT LEN('aa     ')        AS length          -- 2
      ,DATALENGTH('aa     ') AS datalength      -- 7

Last possible solution I see you do sth like:

SELECT 'aa                aaaa' 

-- result in SSMS `aa aaaa`: so when you count you lose all multiple whitespaces

Check query below if returns 100k:

SELECT DATALENGTH(ntext_column)
Community
  • 1
  • 1
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
3

Based on what I've seen, you may just only be able to copy 43679 characters. It is storing all the characters, they're in the db(check this with Select Len(Reponse) From [table] Where... to verify this), and SSMS has problem copying more than when you go to look at the full data.

iceman825
  • 46
  • 1
  • This was my problem solution. I was copying from the DBMS field, but I only got 43679 characters. Why? So I saved the ntext field from datatabase directly to the file and all characters were there. Thanks – Ali reza Soleimani Asl Jan 14 '21 at 07:50
1

For all bytes; Grid result on right click and click save result to file.

Ali Osman Yavuz
  • 389
  • 3
  • 4
0

Can confirm. The actual limit is 43679. Had a problem with a subscription service for a week now. Every data looked good, but it still gave us an error that one of the fields have invalid values, even tho, it got correct values in. It turned out that the parameters was stored in NText and it maxed out at 43679 characters. And because we cannot change the database design, we had to make 2 different subscriptions for the same thing and put half of the entities to the other one.

S.Zsolt
  • 23
  • 4