String Datatypes

Oracle supports four string datatypes, which are summarized in Table 8-1. Which type you should use depends on your answers to the following two questions:

  • Are you working with variable-length or fixed-length strings?

  • Do you wish to use the database character set or the national character set?

Table 8-1. PL/SQL’s string datatypes

Fixed-length

Variable-length

Database character set

CHAR

VARCHAR2

National character set

NCHAR

NVARCHAR2

You will rarely need or want to use the fixed-length CHAR and NCHAR datatypes in Oracle-based applications; in fact, we recommend that you never use these types unless there is a specific requirement for fixed-length strings. See the upcoming Section 8.3 for a description of problems you may encounter when mixing fixed- and variable-length string variables.

The VARCHAR2 Datatype

VARCHAR2 variables store variable-length character strings. When you declare a variable-length string, you must also specify a maximum length for the string, which can range from 1 to 32767 bytes. You may specify the maximum length in terms of characters or bytes, but either way the length is ultimately defined in bytes. The general format for aVARCHAR2 declaration is:

                  variable_name VARCHAR2 (max_length [CHAR | BYTE]);

where:

variable_name

Is the name of the variable you wish to declare.

max_length

Is the maximum length of the variable.

CHAR

Indicates that max_length is expressed in terms of characters.

BYTE

Indicates that max_length represents ...

Get Oracle PL/SQL Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.