Performance on datatype varcahr2(20) vs varchar2(200) - Ask TOM (2024)

The major difference is the maximum length of a varchar2(20) is 20 bytes or characters (depending on how you defined it). The maximum length of a varchar2(200) is 200 bytes/characters:

SQL> create table t ( 2 c20c varchar2(20 char), 3 c20b varchar2(20 byte), 4 c200c varchar2(200 char), 5 c200b varchar2(200 byte) 6 );Table created.SQL>SQL> insert into t values (null, null, null, null);1 row created.SQL>SQL> update t 2 set c20c = lpad('x', 21, 'x');set c20c = lpad('x', 21, 'x') *ERROR at line 2:ORA-12899: value too large for column "CHRIS"."T"."C20C" (actual: 21, maximum: 20)SQL>SQL> update t 2 set c200c = lpad('x', 21, 'x');1 row updated.SQL>SQL> -- 2-byte character, can only fit 10 chars if column is defined with byteSQL> update t 2 set c20b = lpad('é', 11, 'é');set c20b = lpad('é', 11, 'é') *ERROR at line 2:ORA-12899: value too large for column "CHRIS"."T"."C20B" (actual: 33, maximum: 20)SQL>SQL> update t 2 set c200b = lpad('é', 11, 'é');1 row updated.

A varchar2 only uses the space up to the length of the string. So if you store a 1 byte character in a varchar2(200 byte), the other 199 bytes are still available:

https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:123212348063

There are some gotchas to defining a varchar2 with a large value "just in case". For example, if the sum of the lengths for a multi-column index is too big, you'll get an error:

create table t ( x varchar2(4000), y varchar2(4000), z varchar2(4000));create index i on t (x, y, z);SQL Error: ORA-01450: maximum key length (6398) exceeded

View the maximum length of a varchar2 as a constraint. Ask yourself, should it be possible to store strings longer than X?

If the answer is no, then use that in the definition. e.g. many ISO codes are defined as 2 or 3 character strings. So these should be varchar2(2 char) or varchar2(3 char) as needed.

You can find further discussion and opinions in this Oracle-L thread:

https://www.freelists.org/post/oracle-l/RE-bytes-vs-chars

Performance on datatype varcahr2(20) vs varchar2(200) - Ask TOM (2024)

FAQs

What is the difference between VARCHAR2 20 and VARCHAR2 20 char? ›

varchar2(20 char) means you can store 20 characters -- whereas varchar2(20) means you can store 20 bytes.

Should I use VARCHAR or VARCHAR2 in Oracle? ›

Note : There is no difference between VarChar and VarChar2 in Oracle. However, it is advised not to use VarChar for storing data as it is reserved for future use for storing some other type of variable.

What is the difference between char and VARCHAR2 data type? ›

CHAR is a fixed-length data type that pads values with spaces/fillers to meet the specified length. VARCHAR and VARCHAR2 are variable length data types that do not pad values. The length of a value in CHAR/VARCHAR is equal to the number of characters in it or bytes of space occupied by it.

What is the difference between VARCHAR2 and long? ›

The LONG datatype is like the VARCHAR2 datatype, except that the maximum size of a LONG value is 32760 bytes. You use the LONG RAW datatype to store binary data or byte strings. LONG RAW data is like LONG data, except that LONG RAW data is not interpreted by PL/SQL. The maximum size of a LONG RAW value is 32760 bytes.

Why is VARCHAR2 preferred over VARCHAR? ›

Use varchar2 in instead of varchar if empty string and NULL are interchangeable. because it considers empty strings and null strings equally. Despite the fact that both are now used for the same purpose, Oracle advised, “Do not utilise varchar datatype.” However, future usage of varchar might alter.

How many characters can VARCHAR2 20 hold? ›

For instance, a VARCHAR2(20 BYTE) column in an AL32UTF8 database could hold 20 characters from the ASCII range, 10 Latin letters with umlaut, 10 Cyryllic, 10 Hebrew, or 10 Arabic letters (2 bytes per character), or 6 Chinese, Japanese, Korean, or Devanagari (Indic) characters.

What is a disadvantage of using a CHAR data type as opposed to a VARCHAR2 data type? ›

The difference between a CHAR and a VARCHAR is that a CHAR(n) will ALWAYS be N bytes long, it will be blank padded upon insert to ensure this. A varchar2(n) on the other hand will be 1 to N bytes long, it will NOT be blank padded. Using a CHAR on a varying width field can be a pain due to the search semantics of CHAR.

What is the maximum size of VARCHAR2? ›

The maximum length for VARCHAR2 is 32672 BYTE or 8168 CHAR which is the same as the maximum length for VARCHAR of 32672 OCTETS or 8168 CODEUNITS32.

Why use VARCHAR instead of string? ›

The short answer is: VARCHAR is variable length, while CHAR is fixed length. CHAR is a fixed length string data type, so any remaining space in the field is padded with blanks.

Is it better to use char or VARCHAR? ›

Use char when the sizes of the column data entries are consistent. Use varchar when the sizes of the column data entries vary considerably. Use varchar(max) when the sizes of the column data entries vary considerably, and the string length might exceed 8,000 bytes.

Is VARCHAR better than char? ›

One of the main advantages of using VARCHAR over CHAR is that it can save space in the database because it only uses the amount needed to store the actual data and not a fixed amount of space for all rows, regardless of the length of the stored string.

Why do we use VARCHAR2 in SQL? ›

The VARCHAR2 data type specifies a variable-length character string in the database character set. You specify the database character set when you create your database. When you create a table with a VARCHAR2 column, you must specify the column length as size optionally followed by a length qualifier.

What is the limitation of VARCHAR2 in Oracle? ›

You must specify size for VARCHAR2 . Minimum size is 1 byte or 1 character. Maximum size is: 32767 bytes or characters if MAX_STRING_SIZE = EXTENDED.

What is the difference between long VARCHAR and VARCHAR2? ›

The VARCHAR2 datatype takes a required parameter that specifies a maximum size up to 32767 bytes. We use the LONG datatype to store variable-length character strings. The LONG datatype is like the VARCHAR2 datatype, except that the maximum size of a LONG value is 32760 bytes.

Do we need to specify size with VARCHAR2? ›

The maximum size of the VARCHAR2 data type is 32,767 bytes. When you create a VARCHAR2 column, you must specify the maximum length for the VARCHAR2 column. Although the actual minimum string allowed for storage in a column of this type is a zero-length string (''), the maximum length must be at least 1 byte.

What is the difference between VARCHAR2 and VARCHAR2 char? ›

The difference between a CHAR and a VARCHAR is that a CHAR(n) will ALWAYS be N bytes long, it will be blank padded upon insert to ensure this. A varchar2(n) on the other hand will be 1 to N bytes long, it will NOT be blank padded. Using a CHAR on a varying width field can be a pain due to the search semantics of CHAR.

What is the difference between char 20 and VARCHAR 20? ›

Char is best used when you expect the data values in a column to be the same length. On the other hand, varchar is best used when you expect the data values in a column to be of variable length.

What is VARCHAR2 20 in SQL? ›

The VARCHAR2 data type specifies a variable-length character string in the database character set. You specify the database character set when you create your database. When you create a table with a VARCHAR2 column, you must specify the column length as size optionally followed by a length qualifier.

What is the difference between VARCHAR2 10 and VARCHAR2 10 char? ›

So varchar2(10 char) is explicit. This can store up to 10 characters. Varchar2(10) is implicit. It may store 10 bytes or 10 characters, depending on the DB configuration.

Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 5959

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.