0

i'm currently developing for a big company, and they ask me to create a database for the project following some guidelines, but i have a small question about char and varchar.

I have a string that can be either 8 or 11 char and I would like to know which is the best solution:

myColumn varchar(11) or myColumn char(11)

At first i was thinking about the second one, but char is for fixed length no?

Thanks for the answers.

Nicolas
  • 1
  • 4
  • http://www.c-sharpcorner.com/uploadfile/cda5ba/difference-between-char-nchar-varchar-and-nvarchar-data-ty/. This should give you an idea on what to use. – Vikram Nov 29 '16 at 10:20
  • See this fully-detailed answer. http://stackoverflow.com/a/59686/6211175 – elif erdil Nov 29 '16 at 10:24
  • @PareshJ instead of flaming, try to give a decent answer. – Jordec Nov 29 '16 at 10:29
  • But as far as I know varchar would be a better option, since it will allocate only the memory you need, while char will take the full length. – Jordec Nov 29 '16 at 10:34
  • In this case, CHAR is best option, why ? if string is 8 > char (11) allocates 11 byte VS varchar(11) allocates 10 byte (8 + 2 overhead) ...... if string is 11 > char (11) allocates 11 byte VS varchar(11) allocates 13 byte (11 + 2 overhead) besides char bit faster than vahchar – ahmed abdelqader Nov 29 '16 at 10:40

3 Answers3

0

char(11) can hold string with length upto 11 chars, but even if you have only 2 chars, it will take space for the entire 11 chars(will fill the rest with space. where as varchar(11) does hold the strings with same length, but will not reserve space.

What is the major difference between Varchar2 and char

Community
  • 1
  • 1
Vilsad P P
  • 1,529
  • 14
  • 23
0

CHAR Data Type is a Fixed Length Data Type. For example, if you declare a column of CHAR (11) data type, then it will always take 11 bytes irrespective of whether you are storing 1 character or 11 characters in this column.

On the other hand, VARCHAR is a variable length Data Type. For example, if you declare a column of VARCHAR (11) data type, it will take the number of bytes equal to the number of characters stored in this column. So, in this variable/column if you are storing only one character then it will take only one byte and if we are storing 11 characters then it will take 11 bytes. And in this example, as you have declared a column as VARCHAR (11), so we can store max 11 characters in this column.

https://msdn.microsoft.com/en-in/library/ms176089.aspx

Ayyappa P
  • 27
  • 7
0

varchar(11) is more preferable because, memory allocation is dynamically (eg. Suppose string length is 8 then memory allocation for just 8 character not for all). but in case of char(11) memory allocated for all cell.

Anuj Kumar
  • 29
  • 1
  • 6