Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining Nvarchar length

I've read all about varchar versus nvarchar. But I didn't see an answer to what I think is a simple question. How do you determine the length of your nvarchar column? For varchar it's very simple: my Description, for example, can have 100 characters, so I define varchar(100). Now I'm told we need to internationalize and support any language. Does this mean I need to change my Description column to nvarchar(200), i.e. simply double the length? (And I'm ignoring all the other issues that are involved with internationalization for the moment.)

Is it that simple?

like image 443
Kane Jeeves Avatar asked Jan 21 '14 13:01

Kane Jeeves


People also ask

How can I check nvarchar length?

But if the storage length is desired, you can drag the table name into the query window using SSMS, highlight it, and use 'Alt-F1' to see the defined lengths of each column. If you insert ASCII characters into the varchar and nvarchar fields, it will allow you to put 10 characters into all of them.

What is length in nvarchar?

nvarchar [ ( n | max ) ] n defines the string size in byte-pairs, and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^30-1 characters (2 GB).

How long is nvarchar 255?

nvarchar(255) (in SQL Server) stores 255 Unicode characters (in 510 bytes plus overhead).

How many characters does nvarchar 50 hold?

The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.


2 Answers

Generally it is the same as for varchar really. The number is still the maximum number of characters not the data length.

nvarchar(100) allows 100 characters (which would potentially consume 200 bytes in SQL Server).

You might want to allow for the fact that different cultures may take more characters to express the same thing though.

An exception to this is however is if you are using an SC collation (which supports supplementary characters). In that case a single character can potentially take up to 4 bytes.

So worst case would be to double the character value declared.

like image 57
Martin Smith Avatar answered Oct 22 '22 18:10

Martin Smith


From microsoft web site:

A common misconception is to think that NCHAR(n) and NVARCHAR(n), the n defines the number of characters. But in NCHAR(n) and NVARCHAR(n) the n defines the string length in byte-pairs (0-4,000). n never defines numbers of characters that can be stored. This is similar to the definition of CHAR(n) and VARCHAR(n). The misconception happens because when using characters defined in the Unicode range 0-65,535, one character can be stored per each byte-pair. However, in higher Unicode ranges (65,536-1,114,111) one character may use two byte-pairs. For example, in a column defined as NCHAR(10), the Database Engine can store 10 characters that use one byte-pair (Unicode range 0-65,535), but less than 10 characters when using two byte-pairs (Unicode range 65,536-1,114,111). For more information about Unicode storage and character ranges, see

https://docs.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql?view=sql-server-ver15

like image 39
Musa Caglar Avatar answered Oct 22 '22 17:10

Musa Caglar