String Datatypes
Oracle supports four string datatypes, summarized in the following table. 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 want to use the database character set or the national character set?
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, I recommend that you never use these types unless there is a specific requirement for fixed-length strings. See Mixing CHAR and VARCHAR2 Values for a description of problems you may encounter when mixing fixed- and variable-length string variables. (The NCHAR and NVARCHAR2 datatypes are discussed in Chapter 25.)
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 32,767 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 a VARCHAR2 declaration is:
variable_name
VARCHAR2 (max_length
[CHAR | BYTE]);
where:
- variable_name
Is the name of the variable you want 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, 5th 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.