nvarchar vs varchar in SQL Server: which to use

nvarchar vs varchar: nvarchar stores Unicode, 2 bytes a character; varchar stores one code page, 1 byte. What each costs, and runs where data goes missing.

What is the difference between nvarchar and varchar?

nvarchar stores Unicode (multilingual) text; varchar stores non-Unicode text in one code page, the one its collation names. nvarchar encodes any character in any language as UTF-16: two bytes for almost every character, and four for the rare ones outside the Basic Multilingual Plane, such as emoji. varchar holds only the characters of its code page, one byte each for Latin text. So the same English name takes twice the space in nvarchar, and a name in Greek or Polish fits in nvarchar and may not fit in varchar at all.

The number in brackets counts storage, not characters. In varchar(n) it is bytes, 1 to 8,000; in nvarchar(n) it is byte-pairs, 1 to 4,000. Past those limits you write max, which holds up to 2 GB in either type (char and varchar, nchar and nvarchar). Ask for more and SQL Server stops before running anything:

DECLARE @Outline nvarchar(8000);
Msg 2717, Level 16, State 2, Line 1
The size (8000) given to the parameter '@Outline' exceeds the maximum allowed (4000).

The short answer: use nvarchar for text people write, and varchar only for codes your own system issues in a fixed alphabet. Two tables from the university database template follow that split:

CREATE TABLE dbo.Program (
  ProgramId int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Program PRIMARY KEY,
  Code      varchar(10) NOT NULL
    CONSTRAINT AK_Program_Code UNIQUE,
  Name      nvarchar(200) NOT NULL
);
CREATE TABLE dbo.Student (
  StudentId     int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Student PRIMARY KEY,
  StudentNumber varchar(20) NOT NULL
    CONSTRAINT AK_Student_StudentNumber UNIQUE,
  FullName      nvarchar(200) NOT NULL,
  ProgramId     int NOT NULL,
  CONSTRAINT FK_Student_Program_ProgramId
    FOREIGN KEY (ProgramId)
    REFERENCES dbo.Program (ProgramId)
);
dbo.Program with primary key ProgramId, alternate key Code and Name; dbo.Student with primary key StudentId, alternate key StudentNumber, FullName and foreign key ProgramId; a line from Program to Student.
The codes the university issues are varchar, the names are nvarchar; each type shows at the right of its column.

Why names turn into question marks

An nvarchar column keeps only the characters that reach it. A string literal without the N prefix is varchar, so SQL Server converts it to the database’s default code page before the column ever sees it. Here are two students, each inserted once without N and once with it:

INSERT INTO dbo.Program (Code, Name)
VALUES ('CS-BSC', N'Computer Science');

INSERT INTO dbo.Student (StudentNumber, FullName, ProgramId)
VALUES ('S-1001', 'Łukasz Wiśniewski', 1),
       ('S-1002', N'Łukasz Wiśniewski', 1),
       ('S-1003', 'Δήμητρα Παππά', 1),
       ('S-1004', N'Δήμητρα Παππά', 1);

SELECT StudentNumber, FullName,
  LEN(FullName) AS Characters,
  DATALENGTH(FullName) AS Bytes
FROM dbo.Student
ORDER BY StudentNumber;

The database runs on SQL_Latin1_General_CP1_CI_AS, code page 1252. Every insert succeeds, and no message warns about anything. The rows read back:

  • S-1001, the Polish name without N: stored as “Lukasz Wisniewski”. The two letters code page 1252 doesn’t have were swapped for the nearest plain ones, so the name looks fine and is wrong.
  • S-1002, the same name with N: stored as written.
  • S-1003, the Greek name without N: six of its twelve letters became question marks, and the rest became look-alikes: “t” for tau, “a” for alpha, “p” for pi, the micro sign for mu.
  • S-1004, the same name with N: stored as written.

The damaged rows still have 17 and 13 characters and the same byte counts as the good ones, so a length check passes them too. Microsoft’s documentation says as much: data lost in a code page translation isn’t reported. The same happens with a varchar variable or parameter on the way into an nvarchar column.

What a UTF-8 collation changes for varchar

From SQL Server 2019, a collation whose name ends in _UTF8 makes varchar a Unicode type too, stored as UTF-8 (UTF-8 support). To try it, create a second database with CREATE DATABASE UniversityUtf8 COLLATE Latin1_General_100_CI_AS_SC_UTF8 and give its student table a varchar name:

CREATE TABLE dbo.Student (
  StudentId     int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Student PRIMARY KEY,
  StudentNumber varchar(20) NOT NULL
    CONSTRAINT AK_Student_StudentNumber UNIQUE,
  FullName      varchar(200) NOT NULL
);
dbo.Student with primary key StudentId, alternate key StudentNumber and FullName as varchar(200); no other tables.
FullName is varchar(200) here: this database's UTF-8 collation is what lets it hold any name.
INSERT INTO dbo.Student (StudentNumber, FullName)
VALUES ('S-1001', 'Łukasz Wiśniewski'),
       ('S-1003', 'Δήμητρα Παππά'),
       ('S-1005', 'Emma Hartley');

SELECT StudentNumber, FullName,
  LEN(FullName) AS Characters,
  DATALENGTH(FullName) AS Bytes
FROM dbo.Student
ORDER BY StudentNumber;

No N this time, and both names come back as written, because the database’s default code page is now UTF-8. The bytes change too: the Polish name takes 19 bytes instead of 34 in nvarchar, the Greek one 25 instead of 26, and “Emma Hartley” 12 instead of 24. UTF-8 spends one byte on ASCII, two on most Latin, Greek and Cyrillic letters, and three on Chinese, Japanese or Korean, where UTF-16 needs two.

n still counts bytes, though. A varchar(10) with a UTF-8 collation holds ten ASCII characters but only five Greek ones, and the sixth gives you the string or binary data would be truncated error. The type’s name also says nothing about the encoding: varchar(200) holds any name in this database and loses some in the first one.

Comparing an nvarchar parameter with a varchar column

Mixing the two types in one comparison has a cost that shows up only in the query plan. Here is a lookup by student number, with the variable typed nvarchar:

DECLARE @StudentNumber nvarchar(20) = N'S-1002';

SELECT StudentId, FullName
FROM dbo.Student
WHERE StudentNumber = @StudentNumber;

It finds the student. But when an operator combines two data types, SQL Server first converts the one with the lower data type precedence to the higher one, and nvarchar ranks above varchar. So the column is converted, row by row, not the variable.

With 50,000 more students loaded, the plan in the University database scans the whole AK_Student_StudentNumber index, applying CONVERT_IMPLICIT(nvarchar(20), StudentNumber) to every entry: 133 logical reads. The same query with a varchar(20) variable seeks the index instead and needs 4. The SQL collation is why: it sorts varchar and nvarchar by different rules, so an index in varchar order can’t be searched for an nvarchar value. In UniversityUtf8, whose collation is a Windows one, the optimizer still seeks, through a range it computes from the variable, and checks the converted value on the rows it finds.

The other way round is cheap. A varchar variable against an nvarchar column converts only the variable, and the index seek stays. Microsoft’s client documentation gives the fix for the expensive direction: match the parameter’s type to the column. That means every query has to know which string columns are varchar, which is one more reason to keep them few. Dates have the same trap: in datetime2 vs datetime, a datetime2 parameter misses a row stored as datetime.

A foreign key whose type differs from its parent’s

The bursar’s office records payments by student number. If someone types the new column as nvarchar, because “strings are nvarchar here”, the foreign key to dbo.Student fails:

CREATE TABLE dbo.Payment (
  PaymentId     int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Payment PRIMARY KEY,
  StudentNumber nvarchar(20) NOT NULL,
  Amount        decimal(10, 2) NOT NULL,
  PaidOn        date NOT NULL,
  CONSTRAINT FK_Payment_Student_StudentNumber
    FOREIGN KEY (StudentNumber)
    REFERENCES dbo.Student (StudentNumber)
);
dbo.Program, dbo.Student with alternate key StudentNumber as varchar(20), and dbo.Payment with StudentNumber as nvarchar(20), Amount and PaidOn; a line from Program to Student and none from Student to Payment.
No line from dbo.Student to dbo.Payment: its StudentNumber is nvarchar(20) and the key it points at varchar(20), a pair SQL Server won't tie with a foreign key, so the diagram has none.
Msg 1778, Level 16, State 1, Line 1
Column 'dbo.Student.StudentNumber' is not the same data type as referencing column 'Payment.StudentNumber' in foreign key 'FK_Payment_Student_StudentNumber'.
Msg 1750, Level 16, State 1, Line 1
Could not create constraint or index. See previous errors.

SQL Server wants the child column to match the parent key exactly. A different type, nvarchar or char, raises Msg 1778. The right type with another length, varchar(30) or varchar(10), raises Msg 1753, and a different collation Msg 1757. The error ends the batch, and dbo.Payment isn’t created at all. The foreign key here points at the alternate key on StudentNumber, which is allowed because the key is unique (when it isn’t, you get no primary or candidate keys in the referenced table). The fix is the parent’s type:

CREATE TABLE dbo.Payment (
  PaymentId     int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Payment PRIMARY KEY,
  StudentNumber varchar(20) NOT NULL,
  Amount        decimal(10, 2) NOT NULL,
  PaidOn        date NOT NULL,
  CONSTRAINT FK_Payment_Student_StudentNumber
    FOREIGN KEY (StudentNumber)
    REFERENCES dbo.Student (StudentNumber)
);
dbo.Program, dbo.Student with alternate key StudentNumber, and dbo.Payment with foreign key StudentNumber; a line from Program to Student and one from Student to Payment.
The child column now has its parent's type, varchar(20), and the line from dbo.Student is there.

Changing the parent instead is harder once the key is in use. ALTER COLUMN StudentNumber nvarchar(20) on dbo.Student now fails with Msg 5074 for the alternate key and for the foreign key, then Msg 4922: both have to be dropped, the column changed, and both made again. Settle a key’s type before the first table points at it.

Which one to use, column by column

The university template follows these rules, and they hold for most schemas:

  • nvarchar for anything a person types or another system sends: names, titles, email addresses, notes. You don’t control their alphabet.
  • varchar for codes the database’s own owners issue: Course.Code, Student.StudentNumber, Term.Code. They are ASCII by design, and as keys they travel into other tables and indexes, where half the bytes adds up. Keep the parameters that look them up varchar too.
  • A database created with a UTF-8 collation, on SQL Server 2019 or later, can use varchar for everything and save space on English text. Choose it for the whole database when you create it, and size the columns in bytes.
  • If in doubt, nvarchar. Extra bytes cost disk and memory; a name stored as question marks is gone.

How ERDsmith helps

Every failure above was written into a script before SQL Server saw it: a key typed one way in the parent and another in the child, a type decided per column with no rule behind it. In ERDsmith the types sit on the diagram beside every column, and a key’s type is set once, on the parent:

  • A relationship gives the child its foreign key column with the parent column’s type, length, precision, scale and collation, so dbo.Payment gets varchar(20) without anyone typing it.
  • A child column’s type is locked while the relationship holds it, with the reason under the field: change it on the parent.
  • Changing a parent key column’s type writes the new type to every column that references it.
  • A script whose foreign key column differs from its key in type or length is drawn without that relationship, as the first dbo.Payment was: ERDsmith never builds one SQL Server would refuse with 1778 or 1753.