Summary: In this article, you will learn What is NVARCHAR data type and how to use the SQL NVARCHAR to store variable-length, Unicode string data.
Definition and Usage
NVARCHAR data type in SQL Server is used for the purpose to store variable-length and Unicode string data. To use the SQL NVARCHAR data type to define variable, columns, and parameters variable length characters. this types are variable in length. Its take up more memory than the characters stored. This differs from the SQL Server CHAR type.
Its stores up to 4000 characters with every character taking two(2) bytes. furthermore, This is well favorable for storing the extended character set data, such as John.
The following shows the syntax:
In this syntax:
Here, n defines the string length, which range belong to from 1 to 4,000. Furthermore, if you do not specify the string length, then its default value is 1.
Ulternative way to declare a SQL NVARCHAR column is to use the following syntax:
In this syntax:
max is defined the maximum storage size in bytes which is 2^31-1 bytes (2 GB).
Basically, the actual storage size in bytes of a NVARCHAR value is two(2) times the number of characters entered plus two(2) bytes.
Note:Important Note: The ISO synonyms of NVARCHAR are NATIONAL CHARACTER VARYING or NATIONAL CHAR VARYING thus you’ll be able to use them interchangeably in the variable declaration or column data definition.
Defining SQL NVARCHAR Types
Here, we create a Student table with FirstName as 30 in length, LastName 40 in length and City 50 in length:
CREATE TABLE Student(
Here we declare a variable:
These definitions are like VARCHAR definitions. The main difference is the columns take up twice the space!
SQL SERVER NVARCHAR Data Type Example
Check out this query having FirstName define as NVARCHAR: