Nvarchar Data Type
Nvarchar strings are variable-length strings, stored as a 2-byte (smallint) length specifier followed by data. In uncompressed tables, nvarchar columns occupy their declared length. For example, if ABC is entered into an nvarchar(5) column, the stored result is:
'03ABCxx'
where 03 is a 2-byte length specifier, ABC is three bytes of data, and xx represents two bytes containing unknown (and irrelevant) data.
If the column is nullable, nvarchar columns require an additional byte of storage.
In compressed tables, nvarchar columns are stripped of trailing data. For example, if “ABC” is entered into an nvarchar(5) column in a compressed table, the stored result is:
'03ABC'
The nvarchar data type can contain any character, including non-printing characters and the ASCII null character ('\0').
Blanks are significant in the nvarchar data type. For example, the following two nvarchar strings are not considered equal:
'the store is closed'
and
'thestoreisclosed'
If the strings being compared are unequal in length, the shorter string is padded with trailing blanks until it equals the length of the longer string.
For example, consider the following two strings:
'abcd\001'
where '\001' represents one ASCII character (ControlA) and
'abcd'
If they are compared as nvarchar data types, then
'abcd' > 'abcd\001'
because the blank character added to 'abcd' to make the strings the same length has a higher value than ControlA ('\040' is greater than '\001').
Last modified date: 08/28/2024