What type of data can I choose to store plain text in Microsoft SQL Server 2008?

For example, there is a string field name in my C # (Linq-to-SQL); What data type should this SQL field have?

VARCHAR? Nchar? I'm confused.

+6
string c # sql sql-server
source share
4 answers

SQL Server 2005+ equivalent

The best SQL type equivalent to C # String is nvarchar(max) . It stores exactly the same amount of text as an instance of a C # String object. But be careful. Choosing other types of [n][var]char would be more optimal in the data warehouse (that is, they could work much faster), but choosing one of them depends largely on the data you want to save.

  • char(n) is for strings of fixed length (length n), so if your strings are not different in size, you should choose this type (for example, product keys)
  • varchar(n) used for variable string lengths to length n (i.e. email addresses)
  • varchar(max) used for variable-length strings of any length up to 2G characters, basically the same as the C # String type (i.e. blog content).
  • preend n to these types, and your data is stored in double bytes (unicode), so your strings are more versatile in multilingual applications

SQL Server 2005 Preview

Prior to SQL Server version 2005, the [n]varchar(max) types were not supported. Instead, you had to use the [n]text types, but they were saved differently, so they were much slower compared to the [n][var]char alternatives, and they were also more cumbersome for developers to process and manipulate them.

+10
source share

It can be any type of SQLServer that stores string data. So, NVarchar, Varchar, Char, NChar.

In general, I decided to do something related to the text of NVarchar, unles im 100%, of course, you only need a set of Latin charaters, in which case you can safely go with Varchar.

+1
source share

I always prefer to choose nvarchar: Differences between varchar and nvarchar in SQL Server

+1
source share
  • Varchar is the most common, but it is used only for ASCII text.
  • NVarchar is the default for storing strings as it works with Unicode.
  • Char - fixed char length. This is pretty rare. Almost no one uses it anymore.
  • NChar is a fixed-length unicode char. I have never seen it used.
  • Text and NText are unlimited binary storage for huge strings. They cannot be indexed, so you can avoid them.
+1
source share

All Articles