HomePagev2 Forums SQL Discussions CHAR and VARCHAR2 datatypes

  • CHAR and VARCHAR2 datatypes

     Trever Ehrlich updated 2 years ago 4 Members · 4 Posts
  • Badri Rajasekhar Reddy Rajasekhar Reddy

    Member
    September 12, 2020 at 7:39 pm

    Community:

    Please can you help us understand with examples What is the difference between CHAR and VARCHAR2 datatype in SQL..?

     

  • Naimisha Chekka

    Member
    September 13, 2020 at 4:56 am

    The difference is that CHAR(n) will ALWAYS be n bytes long. If the string length is <n, it will be blank padded upon insert to ensure a length of n. A VARCHAR2(n) on the other hand will be 1 to n bytes long. A shorter string stored as VARCHAR2 will NOT be blank padded.

     
    <p style=”text-align: right;”>For example, suppose you store the string ‘ORATABLE’ in a CHAR(20) field and a VARCHAR2(20) field. The CHAR field will use 22 bytes (2 bytes for leading length). The VARCHAR2 field will use 10 bytes only (8 for the string, 2 bytes for leading length).</p>
    I hope your doubt is clear…

  • Satish Vadlamani

    Administrator
    September 13, 2020 at 11:57 am

    Great explanation.

  • Trever Ehrlich

    Administrator
    September 14, 2020 at 11:04 pm

    But let’s think about the processing overhead: which one is more efficient? Or is one better than the other?

Log in to reply.

Original Post
0 of 0 posts June 2018
Now