Search This Blog

Tuesday, December 21, 2010

Differences between different datatypes in SQL Server

Differences between varchar and nvarchar in SQL Server

VARCHAR is an abbreviation for variable-length character string. It's a string of text characters that can be as large as the page size for the database table holding the column in question. The size for a table page is 8,196 bytes, and no one row in a table can be more than 8,060 characters. This in turn limits the maximum size of a VARCHAR to 8,000 bytes.

The "N" in NVARCHAR means uNicode. Essentially, NVARCHAR is nothing more than a VARCHAR that supports two-byte characters. The most common use for this sort of thing is to store character data that is a mixture of English and non-English symbols — in my case, English and Japanese.


Differences between char and varchar in SQL Server
char is used for fixed length memory storage whereas varchar is used for variable lenght memory storage.
Fox Example if we have char(10) then we can store 10 bit value in it but if we store only 6 bit value in it then rest of the 4 bit space is goes wasted. this limitation is overcome by varchar.
In varchar if we use less space than define space the rest of space is not wasted.

Differences between nchar and nvarchar in SQL Server

NCHAR and NVARCHAR data types are both Unicode character data types with a maximum length of 4,000 characters. The main difference between these 2 data types is that an NCHAR data type is fixed-length while an NVARCHAR is variable-length. If the number of characters entered in an NCHAR data type column is less than the specified column length, spaces are appended to it to fill up the whole length.

Another difference is in the storage size wherein the storage size for NCHAR is two times n bytes while for NVARCHAR is two times the number of characters entered (in bytes).

Differences between varchar and nvarchar in SQL Server 

VARCHAR and NVARCHAR data types are both character data types that are variable-length. Below is the summary of the differences between these 2 data types:

VARCHAR Character Data Type:- Non-Unicode Data
NVARCHAR Character Data Type:- Unicode Data

VARCHAR Maximum Length:- 8,000
NVARCHAR Maximum Length:- 4,000

VARCHAR Character Size:- 1 byte
NVARCHAR Character Size:- 2 byte

VARCHAR Storage Size:- Actual Length (in bytes)
NVARCHAR Storage Size:- 2 times Actual Length (in bytes)

You would use NVARCHAR data type for columns that store characters from more than one character set or when you will be using characters that require 2-byte characters, which are basically the Unicode characters such as the Japanese Kanji or Korean Hangul characters.

No comments:

Post a Comment