What is the difference between char
, nchar
, ntext
, nvarchar
, text
and varchar
in SQL?
I
text
and ntext
are deprecated in favor of varchar(max)
and nvarchar(max)
Text is meant for very large amounts of text, and is in general not meant to be searchable (but can be in some circumstances. It will be slow anyway).
The char/nchar datatypes are of fixed lenghts, and are padded if entered stuff is shorter, as opposed to the varchar/nvarchar types, which are variable length.
The n types have unicode support, where the non-n types don't.
Additionally - text and ntext have been deprecated for varchar(max) and nvarchar(max)
Text is deprecated.
Char is a set value. When you say char(10), you are reserving 10 characters for every single row, whether they are used or not. Use this for something that shouldn't change lengths (For example, Zip Code or SSN)
varchar is variable. When you say varchar(10), 2 bytes is set aside to store the size of the data, as well as the actual data (which might be only say, four bytes).
The N represents uni-code. Twice the space.
I only know between "char" and "varchar".
char: it can allocate memory of specified size whether or not it is filled
varchar: it will allocate memory based on the number of characters in it but it should have some size called maximum size.
N
prefix indicates unicode support and takes up twice the bytes per character of non-unicode.
Varchar
is variable length. You use an extra 2 bytes per field to store the length.
Char
is fixed length. If you know how long your data will be, use char
as you will save bytes!
Text
is mostly deprecated in my experience.
Be wary of using Varchar(max)
and NVarchar(max)
as these fields cannot be indexed.