one character of data

  • Always nchar for those of us in EMEA

  • Being from EU myself I wanted to pick nchar(1) , but chose char(1) instead as this is an American site( hoping for the point:-)).

    I fail to see why you use a varchar when you know that all the space is required and will never be NULL or empty space as stated in the question

  • Respectfully disagree with the answer; I'd actually say varchar(1) is the worst choice. You've got the +2 byte overhead for allowing variable length when the question states the length will always be one, plus if you are going to waste space then use it on the possibility of needing to store Unicode characters. It seems there are two choices for "best" and that would be char(1) or nchar(1)... char(1) because it does the job with the least space unless you need Unicode, in which case nchar(1) is best.

  • bad answer...

    VARCHAR(1) is most definitly not the BEST answer, it should be char(1) or nCHAR(1)

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Noel McKinney (6/5/2009)


    Respectfully disagree with the answer; I'd actually say varchar(1) is the worst choice. You've got the +2 byte overhead for allowing variable length when the question states the length will always be one, plus if you are going to waste space then use it on the possibility of needing to store Unicode characters. It seems there are two choices for "best" and that would be char(1) or nchar(1)... char(1) because it does the job with the least space unless you need Unicode, in which case nchar(1) is best.

    Very well said. There really isn't one "right" answer to this question, but char(1) and nchar(1) are the only "reasonable" answers. This question is a good example of how dangerous bad information can be. The author is under the misconception that the overhead for the varchar length is 1 byte rather than 2. This can make a big difference when determining space requirements for a table with several varchar columns and many rows.

  • I disagree.

    There's overhead to varchar. If you don't need it, you don't put the variable overhead! That's why it's available.

  • Erm... "Either char(1) or varchar(1) may be used. Both would allocate 1 byte of space which would be used. [If there's no data in the variable, varchar would be a better option to use, since char would have allocated and 'used' (padding with space), the space allocated]. "

    Regardless of points (what's a point here and there, right? :-P), I strongly disagree with statements like this being paraded as fact on such a high profile site / source of SQL info. I mean, out of the two even an empty char(1) takes up less space than an empty varchar(1), right?

    --------
    [font="Tahoma"]I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams[/font]

  • I will also use nchar(1).

  • I'm actually horrified that the QOD doesn't get vetted. And it ain't just sour grapes 'cos I didn't get my point! Either the question should have been worded better 'you need to store a single ANSI character', or should have been rejected. Bad question! BAD! nchar(1) is always the correct answer to this question. that is all

  • I believe the answer should be char(1) or nchar(1) depending on the situation. varchar should not be used given the parameters in the question. The choice between char(1) and nchar(1) would depend on the need. char(1) COULD be a better choice -- even than nchar(1), however nchar(1) would universally be a better choice not knowing any more details.

  • I knew it. The author's answer to this question was wrong.

    I knew about the 2 bytes used at the end of a varchar or nvarchar, so I taught char(1) and nchar(1) but that was not an option. Therefore, I choose char(1) since it would be the smallest one to use as long as you don't need unicode.

  • Ignoring the 2 byte overhead for a varchar, char can still work out better for physical storage due to it being a fixed size which can be a benefit later for performance when it comes to reading the rows as they will be a uniform length. So it can be an advantage to waste a bit of space on longer fields but avoid the cost of having rows that are variable lengths.

  • Why would someone consider varchar for a one-byte field? I disagree, though that nvarchar is always the correct answer. It depends on the use of the data.

    So either char(1) or nchar(1) are decent answers but more information is needed to determine which is best in a given scenario.

  • The thing that noone has mentioned is that varchar requires an extra two bytes to store the length of the field. So a varchar(1) column will still occupy three bytes in your database. If the question was looking for the most efficient storage method, in my opinion, it would be char(1).

  • 1."[it] will never be a null or an empty space" -> varchar or nvarchar are out of question.

    2.you didn't specify about character set -> I asume the worst and chose nchar(1)

Viewing 15 posts - 16 through 30 (of 182 total)

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