4
declare @string nvarchar(MAX) = ''

How many chars are available in @string?

edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • Possible duplicate of [What is the maximum characters for the NVARCHAR(MAX)?](https://stackoverflow.com/questions/11131958/what-is-the-maximum-characters-for-the-nvarcharmax) – underscore_d Feb 22 '18 at 12:17
  • @mod: better/older duplicate: https://stackoverflow.com/questions/4270049/what-is-the-maximum-number-of-characters-that-nvarcharmax-will-hold – underscore_d Feb 22 '18 at 12:18

2 Answers2

8

nvarchar(MAX) will hold up to 2GB which is about 1 billion characters since it is unicode

in your case it is 0

also take a look at this, datalength counts storage, len counts characters, for varchar these will be the same

declare @string nvarchar(MAX) = ''
select datalength(@string), LEN(@string)
GO

declare @string nvarchar(MAX) = '1'
select datalength(@string), LEN(@string)
SQLMenace
  • 132,095
  • 25
  • 206
  • 225
3

You have about two billion bytes worth of Unicode characters to play with. From the MSDN documentation for char and varchar:

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179