Difference between char and varchar data types in Sql Server?

 Posted by Sathya4260 on 2/1/2011 | Category: Sql Server Interview questions | Views: 86814 | Points: 40
Answer:

Char:
1.Fixed length memory storage
2.CHAR takes up 1 byte per character
3.Use Char when the data entries in a column are expected to be the same size
5.Ex:
Declare test Char(100);
test="Test" -
Then "test" occupies 100 bytes first four bytes with values and rest with blank data.


VarChar:
1.Variable length memory storage(Changeable)
2.VARCHAR takes up 1 byte per character, + 2 bytes to hold length information
3.varchar when the data entries in a column are expected to vary considerably in size.
4.Ex:
Declare test VarChar(100);
test="Test" -
Then "test" occupies only 4+2=6 bytes. first four bytes for value and other two bytes for variable length information.

Conclusion:
1.When Using the fixed length data's in column like phone number, use Char
2.When using the variable length data's in column like address use VarChar


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Premkand on: 3/3/2011 | Points: 10
Good Example. Its not 4+6. 4+2 and the total length occupied is 6.

Thanks,

Prem
Posted by: Lasoor on: 6/6/2011 | Points: 10
if we enter string that contain 100 word..

will it work in both char and varchar case.

as varchar take 2 byte for length.



Posted by: Chvrsri on: 6/6/2011 | Points: 10
Dear Lasoor,
kindly ask your doubts at our forums section.

http://www.dotnetfunda.com/forums/

Here the responses to the posted questions are only to be added.

Thanks,
Srikanth.

Login to post response