Difference between different string types in SQL Server?

前端 未结 9 1505
情书的邮戳
情书的邮戳 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:54

    The n prefix simply means Unicode. They "n" types work similarly to the plain versions except they work with Unicode text.

    char is a fixed length field. Thus char(10) filled with "Yes" will still take 10 bytes of storage.

    varchar is a variable length field. char(10) filled with "Yes" will take 5 bytes of storage (there is a 2 byte overhead for using var data types).

    char(n) holding string of length x. Storage = n bytes. varchar(n) holding string of length x. Storage = x+2 bytes.

    vchar and nvarchar are similar except it is 2 bytes per character.

    Generally speaking you should only use char & char (over varchar & nvarchar) when working with fixed or semi-fixed strings. A good example would be a product_code or user_type which is always n characters long.

    You shouldn't use text (or ntext) as it has been deprecated. varchar(max) & nvarchar(max) provides the same functionality.

提交回复
热议问题