difference between char and varchar

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
I'm having trouble understanding what is meant by fixed length strings and variable length.

Is this right?

When declaring a column and its attributes, you can decide on many data types, 2 of which include char and varchar.

With char, it is for fixed length. So you would declare that fixed length when declaring the column. With varchar, there is no max lenth for a string, just the maximum that varchar supports.

I guess the reasoning is, if you need to input character strings, and you know those strings will never be more than 100 characters, you could chose char, and declare it to be 100 characters in size. That way, when creating the data pages for the table rows, the db only needs to allocate enough memory to accomodate that max of 100 characters.

But with varchar, the string length could vary greatly, so it just allocates the same maximum space for every row?

 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
No. varchar only stores actual data+length byte(s), it doesn't reserve space for the max size.

Generally you use varchars.
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Originally posted by: nickbits
No. varchar only stores actual data+length byte(s), it doesn't reserve space for the max size.

Generally you use varchars.

Then what's the difference with char? Does char actually reserve the space for max size?
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Originally posted by: TechBoyJK
Originally posted by: nickbits
No. varchar only stores actual data+length byte(s), it doesn't reserve space for the max size.

Generally you use varchars.

Then what's the difference with char? Does char actually reserve the space for max size?

char fields allocate that amount of space, you can think of them as fixed size records. I char(40) will always take 40 bytes, a varchar(40) will take upto 40 bytes.
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Originally posted by: bsobel
Originally posted by: TechBoyJK
Originally posted by: nickbits
No. varchar only stores actual data+length byte(s), it doesn't reserve space for the max size.

Generally you use varchars.

Then what's the difference with char? Does char actually reserve the space for max size?

char fields allocate that amount of space, you can think of them as fixed size records. I char(40) will always take 40 bytes, a varchar(40) will take upto 40 bytes.

Thank you. So I guess, ideally, if diskspace is a concern, use varchar, but if the records change often and all of a sudden a varchar goes from needing 1byte to 40byte, if a datapage hasn';t already allocated the space, it may need to spread that data out and it won't be orderly again until the db is degragged... right?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: TechBoyJK
Originally posted by: bsobel
Originally posted by: TechBoyJK
Originally posted by: nickbits
No. varchar only stores actual data+length byte(s), it doesn't reserve space for the max size.

Generally you use varchars.

Then what's the difference with char? Does char actually reserve the space for max size?

char fields allocate that amount of space, you can think of them as fixed size records. I char(40) will always take 40 bytes, a varchar(40) will take upto 40 bytes.

Thank you. So I guess, ideally, if diskspace is a concern, use varchar, but if the records change often and all of a sudden a varchar goes from needing 1byte to 40byte, if a datapage hasn';t already allocated the space, it may need to spread that data out and it won't be orderly again until the db is degragged... right?

ANSI compatible padding behavior is also different between char and varchar, so if that matters you'll need to review it. Generally char is useful for fixed length fields, or fields you always want padded out to a fixed length. About the only time I use it is for single character flags like 'Y' and 'N'.
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Correct, fixed size tables are generally faster since the DB can easily calculate where the next record is. Doesnt matter as much with small db's but is important in enterprise scenarios.