T-SQL Tutorial

Difference between Varchar and Nvarchar


Varchar vs Nvarchar

In SQL Server, both VARCHAR and NVARCHAR are data types used to store character data. However, there is a significant difference between the two: VARCHAR is used for non-Unicode character data, while NVARCHAR is used for Unicode character data. Let's explore these differences in more detail:

Varchar data type can store non-Unicode string data.
Varchar stores data at 1 byte per character.
Varchar supports up to 8000 characters.
Nvarchar data type can store Unicode string data.
Nvarchar stores data at 2 bytes per character.
Nvarchar supports up to 4000 characters.


Character Encoding

VARCHAR(Variable Character) stores data in a non-Unicode character set, typically using a single byte per character. This means it is suitable for storing characters from the ASCII character set, which includes English letters, digits, and common symbols.
NVARCHAR(National Variable Character) stores data in a Unicode character set, typically using two bytes per character. Unicode is a standard that can represent characters from virtually all written languages around the world, making NVARCHAR suitable for multilingual data.


Storage Size

Since VARCHAR uses a single byte per character, it generally consumes less storage space than NVARCHAR, which uses two bytes per character. For example, the word "HELLO" would require 5 bytes of storage in VARCHAR (5 characters) but 10 bytes in NVARCHAR (5 characters * 2 bytes each).


Performance Considerations

VARCHAR can be more performant in terms of storage and query processing for non-Unicode data since it consumes less space and requires fewer bytes to be processed. However, the performance difference may not be significant in most cases.


Best Practices

Choose VARCHAR when you are certain that your data will always be in a single-byte character set and no multilingual or special characters will be present.
Choose NVARCHAR when working with multilingual, internationalized data, or when you want to ensure compatibility with various character sets and symbols.


Varchar

USE model;
GO
DECLARE @varVarchar AS varchar(250) = '☥2625';
SELECT @varVarchar as v_var;
GO

Results: ?2625


Nvarchar

USE model;
GO
DECLARE @varNvarchar AS nvarchar(250) = N'☥2625';
SELECT @varNvarchar as n_var;
GO

Results: ☥2625


In summary, the choice between VARCHAR and NVARCHAR in SQL Server depends on the nature of your data and your application's requirements. It's essential to select the appropriate data type to ensure data integrity and efficient storage and retrieval.