Easy question: char or varchar?

  • Hello everyone! I've read the FAQ section and I think it's really good but I couln't find an answer to this:

    When should I use char and when should I use varchar? This is what they say on MSDN:

    Use char when the data values in a column are expected to be consistently close to the same size.

    Use varchar when the data values in a column are expected to vary considerably in size.

    Well, what is "close to the same size" and what is considered "vary considerably"? I came across this problem when I was making the field "UserName". I want the max size to be 32 but I think almost every Username will be 8 to 12 characters. So they vary over 20 characters from the max size. Is this "vary considerably"?

    My first post here. Gimme a good impression

    Edited by - tomiz on 04/30/2002 3:15:23 PM

  • I would say yes. Typically I only use chars for small columns that are either null or fill the column..state abbreviations, things like that. The additional overhead for a varchar isn't severe and I believe the combination of the space efficiency and the ease of working with them make varchars the better choice. In my opinion only of course!

    Andy

  • To piggy-back on what Andy has already said... varchar saves you space, char performs "slightly" better. Is that slight performance increase noticeable? Probably not for most uses.

    The overhead is 2 bytes per varchar column. Pretty small when characters are varying by up to 20 characters. A great article talking a little more about page size and page usage:

    http://qa.sqlservercentral.com/columnists/sjones/pagesize.asp

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://qa.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • And to add in there. If you do not use the size of the column with a char it will add in blanks after and may require trimming in some apps to get things right (Crystal Reports drove me nuts with this).

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

  • Thank you!!

    Now I got this staight

    Thanks again! I really appreciate it!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply