varchar vs char in SQL

Question

What is the difference between CHAR and VARCHAR2 datatype in SQL?

in progress 0
Pragya Rani 55 years 1 Answer 924 views Contributor 0

Answer ( 1 )

  1. If we talk in easy language, CHAR(n) will always be n bytes long. As the string is <n, the rest of the spaces will be a blank padded area to ensure the complete length of 'n'. Whereas VARCHAR2(n) on the other side will only use the necessary amount of bytes for the string. It will automatically shrink to meet the size of the string.
    Eg: Storing "PYTHON" in CHAR(15) will use 17 bytes for the process as 2 bytes are allocated for reading length.
    Same string if stored in VARCHAR(15), only 8 bytes will be consumed as 6 bytes for the string and the rest 2 bytes for reading.
    So, it's advisable to use VARCHAR2 to avoid memory waste. Rest, the usage is situation dependent.

Leave an answer

Browse
Browse