Results 1 to 13 of 13

Thread: Char and Varchar in SQL

  1. #1
    Contributing Member
    Join Date
    May 2006
    Answers
    71

    Char and Varchar in SQL

    What is the difference between char and varchar in SQL data type? Is both the same? If not in which situation one use char and in which situation one use varchar?


  2. #2
    Expert Member
    Join Date
    Jun 2006
    Answers
    410

    Re: Char and Varchar in SQL

    memory allocation is static for char data type variables but in case of varchar memory allocation is dynamic. This is the basic difference between them

    -- James


  3. #3
    Contributing Member
    Join Date
    May 2006
    Answers
    71

    Re: Char and Varchar in SQL

    James it was a good explanation which gave me an insight about the main difference. But could you say me in what situations people prefer to use char.


  4. #4
    Expert Member
    Join Date
    Sep 2006
    Answers
    130

    Re: Char and Varchar in SQL

    Fred,

    I think u have understood the basic difference between char & varchar is that char datatype is used to store fixed length character value & varchar is used to store variable length character value.

    Since ur very much interested in an example, let me explain with an example.
    Lets say the data which has to be stored in Employee_ID is fixed length of 7, then CHAR datatype can be used. CHAR(7) - the length of the value that will be stored will be static & so CHAR is used.
    If variable length data has to be stored like Employee Name, then VARCHAR can be used. The size of the column value can vary so variable length datatype is used.

    *** Innila ***

    Last edited by Innila; 03-08-2007 at 05:11 AM.

  5. #5
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    Re: Char and Varchar in SQL

    Thats nice.
    Can you tell me the difference between varchar and varchar2 in oracle?


  6. #6
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: Char and Varchar in SQL

    Well, VARCHAR can store upto 2000bytes and VARCHAR2 can store upto 4000bytes.
    Also, VARCHAR occupies space for NULLs and VARCHAR2 doesn't. One can say VARCHAR2 is advanced VARCHAR. Apparantly, Orcale has plans of scraping VARCHAR and retaining only VARCHAR2.

    And oracle strongly recommends use of VARCHAR2

    Cheers,
    Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  7. #7
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: Char and Varchar in SQL

    In varchar, you can store 2000 bytes of data...
    In varchar2, you can store 4000 bytes of data

    In future you want add any other further updatation (ie increase the size of data...) not possible in varchar..You can do if you use varchar2.

    I don't know this is correct...According to my knowledge i told this..

    If anybody knows other than this plz post their answer.

    -----------------------------
    suresh


  8. #8
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: Char and Varchar in SQL

    Ohhh already kalayama explained it....

    Just now i saw that one...

    Good answer kalayama.

    ----------------
    suresh


  9. #9
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: Char and Varchar in SQL

    I have never seen you post unnecessarily (When answer is told, you never repeat like most people do). Was wondering what went wrong here when I saw your post.

    Then afterwards I looked at your next post...huh huh! May be we were typing answers at the same time and I just ended up posting it earlier

    No wonder you were chosen member of the month

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  10. #10
    Expert Member
    Join Date
    Nov 2006
    Answers
    518

    Re: Char and Varchar in SQL

    Hay, thanks guys,
    this thread gave me some good info.

    I always used VARCHAR2 thinking there is no difference between VARCHAR and VARCHAR2.

    Though I always used an advanced type (VARCHAR2), I should know the difference. Anyways, now I know it.

    I thank you all again.


  11. #11
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: Char and Varchar in SQL

    Well, good to see that this forum is serving its purpose

    At the end of the day this is what we want. Our knowledge should be shared and improved.

    (I could see that the question was asked by Fred and sutnarcha was benifited by the thread too

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  12. #12
    Moderator
    Join Date
    Sep 2006
    Answers
    920

    Re: Char and Varchar in SQL

    The char is a fixed-length character data type, the varchar is a variable-length character data type.

    Because char is a fixed-length data type, the storage size of the char value is equal to the maximum size for this column. Because varchar is a variable-length data type, the storage size of the varchar value is the actual length of the data entered, not the maximum size for this column.

    You can use char when the data entries in a column are expected to be the same size.
    You can use varchar when the data entries in a column are expected to vary considerably in size.

    Regards,
    Brijesh Jain
    ---------------------------------------------------------
    Connect with me on Skype: jainbrijesh
    Google Plus : jainbrijeshji

  13. #13
    Junior Member
    Join Date
    Feb 2007
    Answers
    17

    Re: Char and Varchar in SQL

    CHAR
    CHAR should be used for storing fix length character strings. String values will be space/blank padded before stored on disk. If this type is used to store varibale length strings, it will waste a lot of disk space.

    VARCHAR
    Currently VARCHAR behaves exactly the same as VARCHAR2. However, this type should not be used as it is reserved for future usage.

    VARCHAR2
    VARCHAR2 is used to store variable length character strings. The string value's length will be stored on disk with the value itself.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact