Friday, 11 July 2014

What is the difference between CHAR and VARCHAR Data types?



Char:
Fixed-length, non-Unicode string data. It defines the string length and must be a value from 1 through 8,000.
Varchar:
Variable-length, non-Unicode string data. It defines the string length and can be a value from 1 through 8,000. 

Difference between CHAR and VARCHAR:

VARCHARS are variable length strings with a specified maximum length. If a string is less than the maximum length, then it is stored verbatim without any extra characters, e.g. names and emails. CHARS are fixed-length strings with a specified set length. If a string is less than the set length, then it is padded with extra characters, e.g. phone number and zip codes. For instance, for a column which is declared as VARCHAR (30) and populated with the word ‘SQL Server,’ only 10 bytes will be stored in it. However, if we have declared the column as CHAR (30) and populated with the word ‘SQL Server,’ it will still occupy 30 bytes in database.
CHAR
VARCHAR
1.     Used to store character string value of fixed length.
Used to store variable length alphanumeric data.
2.     The maximum no. of characters the data type can hold is 255 characters.
The maximum this data type can hold is up to 4000 characters.
3.     It's 50% faster than VARCHAR.
It's slower than CHAR.
4.     Uses static memory allocation.
Uses dynamic memory allocation.

No comments:

Post a Comment