Tech

Oracle Max Length of VARCHAR in SQL and PL/SQL

Oracle Database uses VARCHAR2 to store variable-length character data, including names, addresses, descriptions, and other textual information. The Oracle max length of VARCHAR is 4,000 bytes in standard SQL and 32,767 bytes when extended string sizes are enabled. In PL/SQL, the maximum limit is 32,767 bytes.

Understanding these limits helps developers prevent database errors, manage storage efficiently, and select appropriate data types. However, database configuration and character encoding also influence how much text a column can hold.

What Is VARCHAR in Oracle?

VARCHAR is a character data type designed to store variable-length strings. Unlike fixed-length character types, it does not add unnecessary spaces to shorter values.

Oracle currently treats VARCHAR and VARCHAR2 as synonymous. However, developers should use VARCHAR2 because Oracle may change the behavior of VARCHAR in future releases.

The following example creates a column for storing employee names:

CREATE TABLE employees (
    employee_name VARCHAR2(100)
);

Oracle Max Length of VARCHAR in SQL

In Oracle SQL, the maximum VARCHAR2 length depends on the database version and its MAX_STRING_SIZE configuration. Older Oracle versions support up to 4,000 bytes, while Oracle 12c and later releases support extended string sizes.

Oracle versionStandard limitExtended limit
Oracle 10g4,000 bytesUnavailable
Oracle 11g4,000 bytesUnavailable
Oracle 12c4,000 bytes32,767 bytes
Oracle 19c4,000 bytes32,767 bytes
Oracle 21c4,000 bytes32,767 bytes
Oracle 23ai4,000 bytes32,767 bytes
Oracle 26ai4,000 bytes32,767 bytes

Extended capacity is useful for applications that require longer text fields without immediately switching to CLOB.

Maximum VARCHAR Length in PL/SQL

PL/SQL supports VARCHAR2 variables with a maximum length of 32,767 bytes, regardless of whether standard or extended SQL string sizes are enabled.

Developers can use these variables to process text, perform string operations, and temporarily store information inside procedures and functions.

For example:

DECLARE
    message VARCHAR2(32767);
BEGIN
    message := 'Welcome to Oracle';
    DBMS_OUTPUT.PUT_LINE(message);
END;
/

However, inserting a large PL/SQL string into a smaller SQL column can still produce an error.

SQL vs. PL/SQL VARCHAR Limits

Although SQL and PL/SQL work together, their string storage limits differ. SQL primarily manages persistent database records, whereas PL/SQL supports procedural programming and temporary variables.

FeatureSQLPL/SQL
Standard maximum4,000 bytes32,767 bytes
Extended maximum32,767 bytes32,767 bytes
Primary usageDatabase columnsVariables
Configuration dependentYesNo
Extended mode requiredAbove 4,000 bytesNo

Understanding these differences helps prevent problems when transferring values between application logic and database tables.

Understanding MAX_STRING_SIZE

MAX_STRING_SIZE is an Oracle initialization parameter that determines the maximum SQL capacity of VARCHAR2, NVARCHAR2, and RAW data types.

It provides two configuration options:

  • STANDARD: Supports VARCHAR2 values of up to 4,000 bytes.
  • EXTENDED: Increases the maximum VARCHAR2 capacity to 32,767 bytes.

To check the current configuration in SQL*Plus or SQLcl, run:

SHOW PARAMETER MAX_STRING_SIZE;

Changing this parameter requires administrative privileges and an upgrade procedure. Once EXTENDED mode is enabled, Oracle does not support directly switching back to STANDARD.

VARCHAR2 BYTE vs. CHAR

Oracle allows developers to define VARCHAR2 lengths using either byte or character semantics. The distinction becomes particularly important when working with multilingual applications and Unicode characters.

For example:

VARCHAR2(100 BYTE)
VARCHAR2(100 CHAR)

The first declaration allows 100 bytes, while the second permits up to 100 characters within the applicable byte limit.

In UTF-8 databases, certain characters occupy multiple bytes. Consequently, a column supporting 4,000 bytes may store considerably fewer than 4,000 characters.

How to Check VARCHAR Length

Oracle provides built-in functions for measuring character strings. The LENGTH function returns the number of characters, while LENGTHB calculates the number of bytes.

To identify the longest employee name in a table, use:

SELECT MAX(LENGTH(employee_name))
FROM employees;

To calculate the maximum byte length, use:

SELECT MAX(LENGTHB(employee_name))
FROM employees;

These functions are particularly helpful during database migrations, input validation, and column resizing.

How to Increase VARCHAR2 Length

Developers can increase the maximum length of an existing VARCHAR2 column using the ALTER TABLE statement.

For example:

ALTER TABLE employees
MODIFY employee_name VARCHAR2(200 CHAR);

This statement changes the column’s declared capacity to 200 characters.

For lengths exceeding 4,000 bytes, the database must support extended string sizes. Before increasing column capacity, administrators should review application compatibility, index restrictions, and storage requirements.

Benefits of Using VARCHAR2

VARCHAR2 offers several advantages when designing and managing Oracle databases. Its flexible storage structure makes it suitable for information that varies in length.

The main benefits include:

  • Efficient storage without unnecessary blank padding.
  • Flexible sizing for different application requirements.
  • Support for multilingual and Unicode text.
  • Improved control over input length.
  • Compatibility with SQL string functions.
  • Support for extended textual values.

These features make VARCHAR2 a practical choice for customer information, product descriptions, contact details, and business records.

VARCHAR2 vs. CHAR vs. CLOB

Choosing the appropriate data type depends on the nature and size of the information being stored.

Data typeMaximum SQL sizeSuitable for
VARCHAR24,000 or 32,767 bytesVariable-length text
CHAR2,000 bytesFixed-length values
CLOBMuch larger capacityDocuments and long text

VARCHAR2 works well for everyday database fields, while CHAR suits fixed-length codes. CLOB is generally more appropriate for large documents, articles, and extensive textual records.

Understanding LONG VARCHAR

Oracle Java DB, based on Apache Derby, supports a LONG VARCHAR data type with a maximum capacity of 32,700 characters. Unlike ordinary VARCHAR declarations, it does not require a specified column length.

However, Java DB and Oracle Database are different database systems. The LONG VARCHAR limit in Java DB should not be confused with Oracle Database’s VARCHAR2 storage limits.

Common VARCHAR Length Errors

Oracle generates errors when character values exceed their permitted capacity. Some commonly encountered errors include:

ErrorDescription
ORA-12899Value exceeds column capacity
ORA-00910Declared data type length is too large
ORA-06502PL/SQL value or buffer error
ORA-01489Concatenated string exceeds its limit

Developers can resolve these issues by validating input, adjusting column lengths, checking character encoding, or selecting a more suitable data type.

Best Practices

Choose VARCHAR2 lengths according to actual application requirements rather than assigning the maximum capacity to every column. Use explicit BYTE or CHAR declarations to avoid confusion, particularly when handling international text.

Additionally, validate input before inserting records, review index limitations, and use CLOB when textual content exceeds VARCHAR2 capacity. Always test database configuration changes before applying them to production environments.

Conclusion

The Oracle max length of VARCHAR varies between SQL and PL/SQL. Standard SQL supports 4,000 bytes, while extended configurations and PL/SQL variables support up to 32,767 bytes. Understanding these limitations, choosing appropriate length semantics, and using suitable data types can help developers build reliable databases while avoiding unnecessary storage and compatibility problems.

FAQs

What is the maximum length of VARCHAR in Oracle?

Oracle supports a maximum VARCHAR2 length of 4,000 bytes in standard SQL and 32,767 bytes when extended string sizes are enabled.

Can Oracle VARCHAR2 store more than 4,000 bytes?

Yes, Oracle 12c and later versions support up to 32,767 bytes when the MAX_STRING_SIZE parameter is set to EXTENDED.

What is the maximum VARCHAR2 length in PL/SQL?

The maximum VARCHAR2 variable length in PL/SQL is 32,767 bytes, regardless of the MAX_STRING_SIZE configuration.

Does Oracle VARCHAR2 measure bytes or characters?

Oracle VARCHAR2 supports both BYTE and CHAR length semantics. However, the overall storage capacity remains subject to the maximum byte limit.

What should I use instead of VARCHAR2 for large text?

CLOB is the recommended data type for storing text exceeding 32,767 bytes, such as lengthy documents, articles, and extensive descriptions.

Must Read: Chicken Pot Pie Pot: A Classic Comfort Food

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button