Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My attempt to summarize and correct the existing answers:</p> <p>First, <code>char</code> and <code>nchar</code> will always use a fixed amount of storage space, even when the string to be stored is smaller than the available space, whereas <code>varchar</code> and <code>nvarchar</code> will use only as much storage space as is needed to store that string (plus two bytes of overhead, presumably to store the string length). So remember, "var" means "variable", as in variable space.</p> <p>The second major point to understand is that, <code>nchar</code> and <code>nvarchar</code> store strings using <em>exactly</em> two bytes per character, whereas <code>char</code> and <code>varchar</code> use an encoding determined by the collation code page, which will <em>usually</em> be exactly one byte per character (though there are exceptions, see below). By using two bytes per character, a very wide range of characters can be stored, so the basic thing to remember here is that <code>nchar</code> and <code>nvarchar</code> tend to be a much better choice when you want internationalization support, which you probably do.</p> <p>Now for some some finer points.</p> <p>First, <code>nchar</code> and <code>nvarchar</code> columns <em>always</em> store data using UCS-2. This means that exactly two bytes per character will be used, and any Unicode character in the Basic Multilingual Plane (BMP) can be stored by an <code>nchar</code> or <code>nvarchar</code> field. However, it is not the case that <em>any</em> Unicode character can be stored. For example, according to Wikipedia, the code points for Egyptian hieroglyphs fall outside of the BMP. There are, therefore, Unicode strings that can be represented in UTF-8 and other true Unicode encodings that cannot be stored in a SQL Server <code>nchar</code> or <code>nvarchar</code> field, and strings written in Egyptian hieroglyphs would be among them. Fortunately your users probably don't write in that script, but it's something to keep in mind!</p> <p>Another confusing but interesting point that other posters have highlighted is that <code>char</code> and <code>varchar</code> fields may use two bytes per character for certain characters if the collation code page requires it. (Martin Smith gives an excellent example in which he shows how Chinese_Traditional_Stroke_Order_100_CS_AS_KS_WS exhibits this behavior. Check it out.)</p> <p><strong>UPDATE:</strong> As of SQL Server 2012, there are finally <a href="http://msdn.microsoft.com/en-us/library/ms143726.aspx#Supplementary_Characters">code pages for UTF-16</a>, for example Latin1_General_100_CI_AS_SC, which can truly cover the entire Unicode range.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload