Difference between different string types in SQL Server?

前端 未结 9 1494
情书的邮戳
情书的邮戳 2020-12-29 04:22

What is the difference between char, nchar, ntext, nvarchar, text and varchar in SQL?

I

相关标签:
9条回答
  • 2020-12-29 04:37

    text and ntext are deprecated in favor of varchar(max) and nvarchar(max)

    0 讨论(0)
  • 2020-12-29 04:44

    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.

    0 讨论(0)
  • 2020-12-29 04:45
    • 'n' represents support for unicode characters.
    • char - specifies string with fixed length storage. Space allocated with or without data present.
    • varchar - Varying length storage. Space is allocated as much as length of data in column.
    • text - To store huge data. The space allocated is 16 bytes for column storage.

    Additionally - text and ntext have been deprecated for varchar(max) and nvarchar(max)

    0 讨论(0)
  • 2020-12-29 04:47

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 04:51

    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.

    0 讨论(0)
提交回复
热议问题