Assignment and comparison

The basic operations of SQL are assignment and comparison.

Assignment operations are performed during the execution of statements such as CALL, INSERT, UPDATE, MERGE, FETCH, SELECT INTO, SET host-variable or SET assignment-statement, and VALUES INTO statements. In addition, when a function is invoked or a stored procedure is called, the arguments of the function or stored procedure are assigned. Comparison operations are performed during the execution of statements that include predicates and other language elements such as MAX, MIN, DISTINCT, GROUP BY, and ORDER BY.

The basic rule for both operations is that data types of the operands must be compatible. The compatibility rule also applies to other operations that are described under Rules for result data types.

The following table shows the compatibility of data types for assignments and comparisons for numeric data types.

Table 1. Compatibility of data types for assignments and comparisons with numeric data types. Y indicates that the data types are compatible. N indicates no compatibility. For any number in a column, read the corresponding note at the bottom of the table.
Operand Binary integer Decimal number Floating point Decimal floating point
Binary integer Y Y Y Y
Decimal number Y Y Y Y
Floating point Y Y Y Y
Decimal floating point Y Y Y Y
Character string Start of change1End of change Start of change1End of change Start of change1End of change Start of change1End of change
Graphic string Start of change2End of change Start of change2End of change Start of change2End of change Start of change2End of change
Binary string N N N N
Date N N N N
Time N N N N
Start of changeTimestamp without time zoneEnd of change Start of changeNEnd of change Start of changeNEnd of change Start of changeNEnd of change Start of changeNEnd of change
Start of changeTimestamp with time zoneEnd of change Start of changeNEnd of change Start of changeNEnd of change Start of changeNEnd of change Start of changeNEnd of change
Row ID N N N N
Distinct type 3 3 3 3
XML4 N N N N
Notes:
  1. Start of changeLOBs and bit data are not supported.End of change
  2. Start of changeLOBs are not supported.End of change
  3. A value with a distinct type is comparable only to a value that is defined with the same distinct type. In general, DB2® supports assignments between a distinct type value and its source data type. For additional information, see Distinct type assignments.
  4. For comparison, XML can only be compared using the XMLEXISTS and NULL predicates.

The following table shows the compatibility of data types for assignments and comparisons for string data types.

Table 2. Compatibility of data types for assignments and comparisons with string data types. Y indicates that the data types are compatible. N indicates no compatibility. For any number in a column, read the corresponding note at the bottom of the table.
Operand Character string Graphic string Binary string
Binary integer Start of change7End of change Start of change7End of change N
Decimal number Start of change7End of change Start of change7End of change N
Floating point Start of change7End of change Start of change7End of change N
Decimal floating point Start of change7End of change Start of change7End of change N
Character string Y Y4,5 N3
Graphic string Y4,5 Y N
Binary string N3 N Y
Date 1 1,4 N
Time 1 1,4 N
Start of changeTimestamp without time zoneEnd of change Start of change1End of change Start of change1,4End of change Start of changeNEnd of change
Start of changeTimestamp with time zoneEnd of change Start of change1End of change Start of change1,4End of change Start of changeNEnd of change
Row ID N N N
Distinct type 2 2 2
XML6 N N N
Notes:
  1. The compatibility of datetime values is limited to assignment and comparison:
    • Datetime values can be assigned to string columns and to string variables that are not LOB values, as explained in Datetime assignments.
    • A valid string representation of a date can be assigned to a date column or compared to a date.
    • A valid string representation of a time can be assigned to a time column or compared to a time.
    • A valid string representation of a timestampStart of change(without or with time zone)End of change can be assigned to a timestamp columnStart of change(without or with time zone)End of change or compared to a timestampStart of change(without or with time zone)End of change.
  2. A value with a distinct type is comparable only to a value that is defined with the same distinct type. In general, DB2 supports assignments between a distinct type value and its source data type. For additional information, see Distinct type assignments.
  3. All character strings, even those with subtype FOR BIT DATA, are not compatible with binary strings.
  4. On assignment and comparison from Graphic to Character, the resulting length in bytes is 3 * (LENGTH(graphic-string)), depending on the CCSIDs.
  5. Character strings with subtype FOR BIT DATA are not compatible with Graphic Data.
  6. Character and graphic strings, including LOBs, can be assigned to XML columns. However, XML cannot be assigned to a character or graphic string column. For comparison, XML can only be compared using the XMLEXISTS and NULL predicates.
  7. Start of changeLOBs and bit data are not supported.End of change

The following table shows the compatibility of data types for assignments and comparisons for date, time, and timestamp data types.

Table 3. Compatibility of data types for assignments and comparisons with date, time, and timestamp (without or with time zone) data types. Y indicates that the data types are compatible. N indicates no compatibility. For any number in a column, read the corresponding note at the bottom of the table.
Operand Date Time Start of changeTimestamp without time zoneEnd of change Start of changeTimestamp with time zoneEnd of change
Binary integer N N N N
Decimal number N N N N
Floating point N N N N
Decimal floating point N N N N
Start of changeCharacter stringEnd of change Start of changeYEnd of change Start of changeYEnd of change Start of changeYEnd of change Start of changeYEnd of change
Start of changeGraphic stringEnd of change Start of change2End of change Start of change2End of change Start of change2End of change Start of change2End of change
Binary string N N N N
Date Y N N N
Time N Y N N
Start of changeTimestamp without time zoneEnd of change N N Y Y
Start of changeTimestamp with time zoneEnd of change N N Y Y
Row ID N N N N
Distinct type 1 1 1 1
XML3 N N N N
Notes:
  1. A value with a distinct type is comparable only to a value that is defined with the same distinct type. In general, DB2 supports assignments between a distinct type value and its source data type. For additional information, see Distinct type assignments.
  2. On assignment and comparison from Graphic to Character, the resulting length in bytes is 3 * (LENGTH(graphic-string)), depending on the CCSIDs.
  3. For comparison, XML can only be compared using the XMLEXISTS and NULL predicates.

The following table shows the compatibility of data types for assignments and comparisons for row ID, XML, and distinct data types.

Table 4. Compatibility of data types for assignments and comparisons with row ID, XML, and distinct data types. Y indicates that the data types are compatible. N indicates no compatibility. For any number in a column, read the corresponding note at the bottom of the table.
Operand Row ID XML2 Distinct type
Binary integer N N 1
Decimal number N N 1
Floating point N N 1
Decimal floating point N N 1
Character string N N 1
Graphic string N N 1
Binary string N N 1
Date N N 1
Time N N 1
Start of changeTimestamp without time zoneEnd of change Start of changeNEnd of change Start of changeNEnd of change Start of change1End of change
Start of changeTimestamp with time zoneEnd of change Start of changeNEnd of change Start of changeNEnd of change Start of change1End of change
Row ID Y N 1
Distinct type 1 N Y1
XML2 N Y N
Notes:
  1. A value with a distinct type is comparable only to a value that is defined with the same distinct type. In general, DB2 supports assignments between a distinct type value and its source data type. For additional information, see Distinct type assignments.
  2. For comparison, XML can only be compared using the XMLEXISTS and NULL predicates.

Compatibility with a column that has a field procedure is determined by the data type of the column, which applies to the decoded form of its values.

A basic rule for assignment operations is that a null value cannot be assigned to:

  • A column that cannot contain null values
  • A non-Java host variable that does not have an associated indicator variable

    For a host variable that does have an associated indicator variable, a null value is assigned by setting the indicator variable to a negative value. See Host variables for a discussion of indicator variables.

  • A Java host variable that is a primitive type

    For a Java host variable that is not a primitive type, the value of that variable is set to a Java null value.