Home » Interview Q & A » Q11. What is the difference between the basic datatypes Char and Varchar?

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

Our Categories

Blog Stats

  • 32,288 hits

Calendar

June 2013
M T W T F S S
« May    
 12
3456789
10111213141516
17181920212223
24252627282930

Please visit SQLVERSITY.COM for more details.

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 🙂

Advertisements

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: