The VARCHAR2 and NVARCHAR2 data types are introduced to support applications that use the Oracle VARCHAR2 and NVARCHAR2 data type.
Support for VARCHAR2 and NVARCHAR2 (subsequently jointly referred to as VARCHAR2) is at the database level, and must be enabled before creating the database where support is required. This is achieved by setting the DB2_COMPATIBILITY_VECTOR registry variable to the appropriate value. When a database is created with VARCHAR2 support enabled, the database configuration parameter varchar2_compat is set to ON. After a database is created with VARCHAR2 support enabled, it cannot be disabled for that database, even if the DB2_COMPATIBILITY_VECTOR registry variable is reset. Similarly, all databases created with VARCHAR2 support disabled cannot have VARCHAR2 support enabled, even by setting the DB2_COMPATIBILITY_VECTOR registry variable.
VARCHAR2 data type support is enabled by setting bit position number 6 (0x20) of the DB2_COMPATIBILITY_VECTOR registry variable before creating a database. A new setting for the registry variable does not take effect until after the instance has been stopped and then restarted.
To make use of the NVARCHAR2 data type, the database must be a Unicode database.
The following support is enabled for a DB2® database that has the varchar2_compat database configuration parameter set to ON.
When the VARCHAR2 data type is explicitly encountered in SQL statements, it is implicitly mapped to the VARCHAR data type. The maximum length for VARCHAR2 is the same as the maximum length for VARCHAR (that is, 32 672).
When the NVARCHAR2 data type is explicitly encountered in SQL statements, it is implicitly mapped to the VARGRAPHIC data type. The maximum length for NVARCHAR2 is the same as the maximum length for VARGRAPHIC (that is, 16 336).
Character string literals up to 254 bytes in length have a data type of CHAR. Character string literals longer than 254 bytes have a data type of VARCHAR.
If one operand is... | And the other operand is... | The data type of the result is... |
---|---|---|
CHAR(x) | CHAR(x) | CHAR(x) |
CHAR(x) | CHAR(y) | VARCHAR(z), where x != y and z = max(x,y) |
GRAPHIC(x) | GRAPHIC(x) | GRAPHIC(x) |
GRAPHIC(x) | GRAPHIC(y) | VARGRAPHIC(z), where x != y and z = max(x,y) |
GRAPHIC(x) | CHAR(y) | VARGRAPHIC(z), where z = max(x,y) |
If the result type for the IN list of an IN predicate would resolve to a fixed-length string data type and the left operand of the IN predicate is a varying-length string data type, the IN list expressions are treated as having a varying-length string data type.
Character string values (other than LOB values) with a length of zero are generally treated as null values. An assignment or cast of an empty string value to CHAR, NCHAR, VARCHAR, or NVARCHAR produces a null value.
Operands | Combined length attributes | Result |
---|---|---|
CHAR(A) CHAR(B) | <255 | CHAR(A+B) |
CHAR(A) CHAR(B) | >254 | VARCHAR(A+B) |
CHAR(A) VARCHAR(B) | - | VARCHAR(MIN(A+B,32672)) |
VARCHAR(A) VARCHAR(B) | - | VARCHAR(MIN(A+B,32672)) |
CLOB(A) CHAR(B) | - | CLOB(MIN(A+B, 2G)) |
CLOB(A) VARCHAR(B) | - | CLOB(MIN(A+B, 2G)) |
CLOB(A) CLOB(B) | CLOB(MIN(A+B, 2G)) | |
GRAPHIC(A) GRAPHIC(B) | <128 | GRAPHIC(A+B) |
GRAPHIC(A) GRAPHIC(B) | >128 | VARGRAPHIC(A+B) |
GRAPHIC(A) VARGRAPHIC(B) | - | VARGRAPHIC(MIN(A+B,16336)) |
VARGRAPHIC(A) VARGRAPHIC(B) | - | VARGRAPHIC(MIN(A+B,16336)) |
DBCLOB(A) CHAR(B) | - | DBCLOB(MIN(A+B, 1G)) |
DBCLOB(A) VARCHAR(B) | - | DBCLOB(MIN(A+B, 1G)) |
DBCLOB(A) CLOB(B) | DBCLOB(MIN(A+B, 1G)) | |
BLOB(A) BLOB(B) | - | BLOB(MIN(A+B, 2G)) |
If any argument value has a data type of CLOB or BLOB and any argument is the null value, the result is the null value. All three arguments of the REPLACE function must be specified.
In the ALTER TABLE statement or the CREATE TABLE statement, when a DEFAULT clause is specified without an explicit value for a column defined with the VARCHAR or the VARGRAPHIC data type, the default value is a blank character.