The difference between varchar and nvarchar in SQL Server

All topics about maintaining SQL Server from a DBA perspective.
Post Reply
Styler
Posts: 1
Joined: Fri Jul 19, 2019 4:02 pm

Fri Jul 19, 2019 4:04 pm

What is the difference between the NVARCHAR and VARCHAR data types in SQL Server?
Sergios
Posts: 2
Joined: Mon Jul 08, 2019 6:24 pm

Fri Jul 19, 2019 4:06 pm

varchar
Variable-length, non-Unicode character data. The database collation determines which code page the data is stored using.

nvarchar
Variable-length Unicode character data. Dependent on the database collation for comparisons.


An nvarchar column can store any Unicode data. A varchar column is restricted to an 8-bit codepage. Some people think that varchar should be used because it takes up less space. I believe this is not the correct answer. Codepage incompatabilities are a pain, and Unicode is the cure for codepage problems. With cheap disk and memory nowadays, there is really no reason to waste time mucking around with code pages anymore.

All modern operating systems and development platforms use Unicode internally. By using nvarchar rather than varchar, you can avoid doing encoding conversions every time you read from or write to the database. Conversions take time, and are prone to errors. And recovery from conversion errors is a non-trivial problem.

If you are interfacing with an application that uses only ASCII, I would still recommend using Unicode in the database. The OS and database collation algorithms will work better with Unicode. Unicode avoids conversion problems when interfacing with other systems. And you will be preparing for the future. And you can always validate that your data is restricted to 7-bit ASCII for whatever legacy system you're having to maintain, even while enjoying some of the benefits of full Unicode storage.
Post Reply

Social Media