Difference between Varchar and Nvarchar
Key Difference: In SQL server, both refer to data types. Varchar stands for variable length character string. Varchar stores ASCII data, whereas Nvarchar stores UNICODE data.
include("ad4th.php"); ?>Data types play an important role in describing the form of data. It is useful for the storage of data. Two such data types are varchar and nvarchar. Varchar stands for variable length character string. Varchar basically occupies the number of bytes equal to the number of characters that are stored in the column. Varchar is used when non-Unicode characters are to be stored. It allocates the memory depending upon the number of inserted characters. For example, varchar(30) will initially allocate memory of zero characters during the declaration time. However, let us assume that only 20 characters are inserted, then in that case, the memory will be allocated to only those 20 characters.
Nvarchar is quiet the same as varchar. However, it is used to store Unicode characters, and thus one is able to store multiple languages in database. Nvarchar is preferred over varchar, as it does not require encoding conversions for reading from or writing to the database every time. On the other hand, conversions take time and are prone to errors. However, one should only use nvarchar if there is a need to store the data of different languages that is collation which requires two bytes to store a single character.
include("ad3rd.php"); ?>Comparison between Varchar and Nvarchar in Sql Server:
|
Varchar(n) |
Nvarchar(n) |
Definition |
Varchar stores ASCII data |
Nvarchar stores UNICODE data.
|
Number of bytes for each character |
1 |
2 |
Optional Parameter n range |
Optional Parameter n value can be from 1 to 8000.Can store maximum 8000 Non-Unicode characters. |
Optional Parameter n value can be from 1 to 4000.Can store maximum 4000 Unicode/Non-Unicode characters |
Storage |
Maximum 8000 Non-Unicode characters |
Maximum 4000 Unicode/Non-Unicode characters. |
Code page |
Different types of code pages |
Unicode universal code page |
Memory saving |
Fifty percent memory space is saved more, than in comparison to nvarchar |
Less memory saved comparatively. |
Query execution |
Fast |
Comparatively slow |
Image Courtesy: netbeans.org, msdn.microsoft.com
Add new comment