What is the difference between the basic datatypes Char and Varchar?

Char

1. It is fixed memory allocation.

Ex:

—————————————————-

DECLARE @CharTest CHAR(10)

SET @CharTest = ‘ABCD’

SELECT DATALENGTH(@CharTest)

—————————————————-

Output: 10

—————————————————-

2. Takes more memory space compare to Varchar.

3. Query performance is better.

4. It fills the unused memory space with blank spaces.

Varchar

1. It is variable length memory allocation.

Ex:

———————————————————-

DECLARE @VarCharTest  VARCHAR(10)

SET @VarCharTest = ‘ABCD’

SELECT DATALENGTH(@VarCharTest)

———————————————————-

Output: 4

———————————————————-

2. It takes less memory space.

3. Query performance is less compare to Char.

4. It will never fill the unused memory space.

 My suggestion is, better to use Char if you know the length or else go for Varchar :)

Latest Comments
  1. Raja

    Nice website.. but the thing DECLARE @CharTest CHAR(10)

    SET @CharTest = ‘ABCD’ —-> change the quotes to (single quotes)

    SELECT DATALENGTH(@CharTest)

  2. Sekar

    What is the maximum size of Char or Varchar? i hope it is 8000. if yes, then my next question – if i wanna store a string which has more than 8000 characters length then how to do that?

Leave a Reply

Your email address will not be published. Required fields are marked *