DB2 Data types - Rules for result data types
The data types of a result are determined by rules which are applied to the operands in an operation. This topic explains those rules.
These rules apply to:
These rules are applied subject to other restrictions on long strings for the various operations.
The rules involving various data types follow. In some cases, a table is used to show the possible result data types. The LONG VARCHAR and LONG VARGRAPHIC data types continue to be supported but are deprecated and not recommended.
These tables identify the data type of the result, including the applicable length or precision and scale. The result type is determined by considering the operands. If there is more than one pair of operands, start by considering the first pair. This gives a result type which is considered with the next operand to determine the next result type, and so on. The last intermediate result type and the last operand determine the result type for the operation. Processing of operations is done from left to right so that the intermediate result types are important when operations are repeated. For example, consider a situation involving:
CHAR(2) UNION CHAR(4) UNION VARCHAR(3)
A character string value is compatible with another character string value. Character strings include data types CHAR, VARCHAR, and CLOB.
The code page of the result character string will be derived based on the rules for string conversions.
In a Unicode database, if either operand has string units CODEUNITS32 and the derived code page is not 0, the string units of the result character string is CODEUNITS32. If an operand is defined with CODEUNITS32, the other operand cannot be defined as FOR BIT DATA.Otherwise, the string units of the result character string is OCTETS. Special cases apply when the string units of one operand is CODEUNITS32 and the string units of the other operand is OCTETS with a length attribute that exceeds the data type maximum in CODEUNITS32.
A graphic string value is compatible with another graphic string value. Graphic strings include data types GRAPHIC, VARGRAPHIC, and DBCLOB.
The code page of the result graphic string will be derived based on the rules for string conversions.
In a Unicode database, if either operand has string units CODEUNITS32 and the derived code page is not 0, the string units of the result character string is CODEUNITS32. If an operand is defined with CODEUNITS32, the other operand cannot be defined as FOR BIT DATA. Otherwise, the string units of the result character string is OCTETS. Special cases apply when the string units of one operand is CODEUNITS32 and the string units of the other operand is OCTETS with a length attribute that exceeds the data type maximum in CODEUNITS32. In a Unicode database, if either operand has string units CODEUNITS32, or CODEUNITS16, the string units of the result graphic string is CODEUNITS32. Special cases apply when the string units of one operand is CODEUNITS32 and the string units of the other operand is CODEUNITS16 with a length attribute that exceeds the data type maximum in CODEUNITS32.
In a Unicode database, a character string value is compatible with a graphic string value.
The string units of the result graphic string will be CODEUNITS32 if either operand has string units CODEUNITS32, or CODEUNITS16. Special cases apply when the string units of one operand is CODEUNITS32 and the string units of the other operand is OCTETS or CODEUNITS16 with a length attribute that exceeds the data type maximum in CODEUNITS32.
Binary strings are compatible with other binary strings and FOR BIT DATA character strings. Binary strings include BINARY, VARBINARY and BLOB.
Numeric types are compatible with other numeric data types, character-string data types (except CLOB), and in a Unicode database, graphic-string data types (except DBCLOB). Numeric types include SMALLINT, INTEGER, BIGINT, DECIMAL, REAL, DOUBLE, and DECFLOAT.
Datetime data types are compatible with other operands of the same data type or any CHAR or VARCHAR expression that contains a valid string representation of the same data type. In addition, DATE is compatible with TIMESTAMP and the other operand of a TIMESTAMP can be the string representation of a timestamp or a date. In a Unicode database, character and graphic strings are compatible which implies that GRAPHIC or VARGRAPHIC string representations of datetime values are compatible with other datetime operands.
An XML value is compatible with another XML value. The data type of the result is XML.
A Boolean value is compatible with another Boolean value. The data type of the result is BOOLEAN.
If both operands have the same weakly typed distinct type, the result is the distinct type. Otherwise, if any operand is a weakly typed distinct type then the data type of the operand is considered to be the source data type and the result data type is determined based on the combination of built-in data type operands.
A reference type value is compatible with another value of the same reference type provided that their target types have a common supertype. The data type of the result is a reference type having the common supertype as the target type. If all operands have the identical scope table, the result has that scope table. Otherwise the result is unscoped.
A structured type value is compatible with another value of the same structured type provided that they have a common supertype. The static data type of the resulting structured type column is the structured type that is the least common supertype of either column.
For example, consider the following structured type hierarchy,
A / \ B C / \ D E / \ F G
With the exception of INTERSECT and EXCEPT, the result allows nulls unless both operands do not allow nulls.