What is the maximum number of characters that nvarchar(MAX) will hold?

Sql ServerTsql

Sql Server Problem Overview


I'm new to the concept nvarchar(MAX). How many characters will it hold?

Sql Server Solutions


Solution 1 - Sql Server

Max. capacity is 2 gigabytes of space - so you're looking at just over 1 billion 2-byte characters that will fit into a NVARCHAR(MAX) field.

Using the other answer's more detailed numbers, you should be able to store

(2 ^ 31 - 1 - 2) / 2 = 1'073'741'822 double-byte characters

1 billion, 73 million, 741 thousand and 822 characters to be precise

in your NVARCHAR(MAX) column (unfortunately, that last half character is wasted...)

Update: as @MartinMulder pointed out: any variable length character column also has a 2 byte overhead for storing the actual length - so I needed to subtract two more bytes from the 2 ^ 31 - 1 length I had previously stipulated - thus you can store 1 Unicode character less than I had claimed before.

Solution 2 - Sql Server

From char and varchar (Transact-SQL)

> varchar [ ( n | max ) ] > > Variable-length, non-Unicode character > data. n can be a value from 1 through > 8,000. max indicates that the > maximum storage size is 2^31-1 bytes. > The storage size is the actual length > of data entered + 2 bytes. The data > entered can be 0 characters in length. > The ISO synonyms for varchar are char > varying or character varying.

Solution 3 - Sql Server

2^31-1 bytes. So, a little less than 2^31-1 characters for varchar(max) and half that for nvarchar(max).

nchar and nvarchar

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionquakkelsView Question on Stackoverflow
Solution 1 - Sql Servermarc_sView Answer on Stackoverflow
Solution 2 - Sql ServerAdriaan StanderView Answer on Stackoverflow
Solution 3 - Sql ServerThomasView Answer on Stackoverflow