• Register
0 votes
28 views

Difference between char and varchar in SQL server

I want to know the more interesting difference between char and varchar.
1.9k points

Please log in or register to answer this question.

1 Answer

0 votes

Difference between char and varchar in SQL server

Almost everyone knows the difference between CHAR and VARCHAR data types. I will tell you about one more difference which I saw recently.

CHAR is a data type with a fixed length. Suppose you declare a variable or column of CHAR (10) data type. It will take 10 bytes respectively, even if you store 1 character or 10 characters in the variable or column. We can keep maxim 10 characters n the column. The maximum number of characters is 255. It is 50% faster than VARCHAR and uses static memory allocation.

VARCHAR is a variable-length data type. If you have a variable/column of VARCHAR(10), it will take the number of bytes equal to the number of characters stored in the variable/column. In this variable/column, if you are storing only one character, it will take only one byte, and if we are holding 10 characters, it will take 10 bytes. In the example, we declared the variable/column as VARCHAR (10), and we can store max 10 characters in the column. The maximum data type can hold is

-          Pre-MySQL 5.0.3: 255 characters

-          Post-MySQL 5.0.3: 65 535 characters shared for the row.

It is slower than CHAR and uses dynamic memory allocation.

Here is an example:

DECLARE @CharName Char(20) = 'Maria',

 @VarCharName VarChar(20) = 'Maria'

SELECT DATALENGTH(@CharName) CharSpaceUsed,

 DATALENGTH(@VarCharName) VarCharSpaceUsed

I hope it helps.

3.4k points

Related questions

0 votes
1 answer 17 views
17 views
Problem: I have this date format: 2011-09-28 18:01:00 (in varchar), and I want to convert it to DateTime changing to this format 28-09-2011 18:01:00. How can I do it?
asked Apr 20 ummesalma 29.1k points
0 votes
1 answer 14 views
14 views
Problem: Is it fair that it nvarchar supports multibyte characters? If that's the case, is there really no point, another one of storage concerns, using varchars?
asked Mar 17 sasha 16.2k points
0 votes
1 answer 29 views
29 views
Problem: Need your help to solve this >'difference between varchar and nvarchar in sql server' ?
asked Mar 20 Samiur1 38.6k points
0 votes
1 answer 16 views
16 views
Problem: need someone to understand this problem and find out the solution. Difference between char and varchar in sql
asked May 20 Ifra 43.4k points
0 votes
1 answer 11 views
11 views
Problem : How do I solve it: If x is a char, and values is an int array, then values[x]?
asked Jun 23 Sifat55 90.2k points
0 votes
1 answer 8 views
8 views
Problem: Please solve it … Because I am unable to find out the solution .. what is the difference between a and a
asked Apr 18 Ifra 43.4k points
0 votes
1 answer 4 views
4 views
Problem: Converting char array to string in java.
asked 3 days ago akhi 106k points
0 votes
1 answer 1 view
1 view
Problem: Invalid conversion from ‘char’ to ‘char*’
asked Jul 15 nila 103k points
0 votes
1 answer 10 views
10 views
Problem: Invalid conversion from ‘char*’ to ‘char’
asked Jul 15 asha 103k points
0 votes
1 answer 2 views
2 views
Problem : Invalid conversion from const char to char
asked Jul 14 Siam55 90.9k points