Highstead
Highstead

Reputation: 2441

Allocation of space for nvarchar and varchar

I remember from some time ago being told in academia that Oracle would when given a varchar of (9) would allocate space to actually store 16 bytes worth of characters even though it only needed 9. (pre-wide characters)

Is this still a thing(was this ever a thing)? If I allocate nvarchar(63) will it maximize the storage space(assuming null character)? Will it still take the same space as nvarchar(50)? Is there any benefits to indexing?

I am primarily concerned with MSSQL, but would be interested to know of any databases.

Upvotes: 2

Views: 3863

Answers (2)

paparazzo
paparazzo

Reputation: 45096

It does not preallocate space to nvarchar

the space is number of chars * 2 byte + 2 byte for size

if you stored 40 chars both nvarchar(63) and nvarchar(50) would be the same size 82 bytes

the difference is you cannot put 51 chars in nvarchar(50)

Upvotes: 6

benjamin moskovits
benjamin moskovits

Reputation: 5458

SQL Server allocates two bytes for very actual character in a nvarchar string. So for a nvarchar string declared as Nvarchar(10) ='Ben' Sql server will allow names of up to 10 characters and use six bytes to save the string 'Ben.' It will also use some space to save the actual length of the string (I believe two bytes).

Upvotes: 3

Related Questions