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

The CHAR and VARCHAR2 datatypes in SQL are used to store character strings, but they have some key differences in how they handle storage and padding.

SQL

7/31/20241 min read

MacBook Pro with images of computer language codes
MacBook Pro with images of computer language codes

The CHAR and VARCHAR2 datatypes in SQL are used to store character strings, but they have some key differences in how they handle storage and padding.

CHAR

  • Fixed Length: The CHAR datatype is used for fixed-length character strings. When you define a CHAR column with a specified length, it will always store that exact number of characters.

  • Padding: If the length of the string is shorter than the defined length, the remaining space is padded with spaces.

  • Storage Efficiency: This can lead to inefficient storage if the data varies significantly in length.

VARCHAR2

  • Variable Length: The VARCHAR2 datatype is used for variable-length character strings. It stores only the actual characters provided.

  • No Padding: There is no padding with spaces, so it is more storage-efficient for strings of varying lengths.

  • Performance: It can be more performant in terms of storage and retrieval when dealing with variable-length data.

CHAR Example

Suppose we define a table with a CHAR column:

VARCHAR2 Example

Suppose we define a table with a VARCHAR2 column: