cast-specification |--CAST--(--+---+-expression-------+----AS--| data-type |--+---------------------------------+-+--> | +-NULL-------------+ | (1) | | | '-parameter-marker-' '-SCOPE------+-typed-table-name-+-' | | '-typed-view-name--' | +-cursor-cast-specification--------------------------------------------------------+ '-row-cast-specification-----------------------------------------------------------' >--)------------------------------------------------------------| cursor-cast-specification |--parameter-marker--AS--+-CURSOR-----------+-------------------| '-cursor-type-name-' row-cast-specification |--+-row-expression---+--AS----row-type-name--------------------| +-NULL-------------+ '-parameter-marker-' data-type |--+-| built-in-type |----+-------------------------------------| +-distinct-type-name---+ +-structured-type-name-+ '-REF--(type-name2)----' built-in-type |--+-+-SMALLINT----+---------------------------------------------------+--| | +-+-INTEGER-+-+ | | | '-INT-----' | | | '-BIGINT------' | | .-(5,0)-------------------. | +-+-+-DECIMAL-+-+--+-------------------------+----------------------+ | | '-DEC-----' | | .-,0-------. | | | '-+-NUMERIC-+-' '-(integer-+----------+-)-' | | '-NUM-----' '-,integer-' | | .-(53)------. | +-+-FLOAT--+-----------+--+-----------------------------------------+ | | '-(integer)-' | | | +-REAL------------------+ | | | .-PRECISION-. | | | '-DOUBLE--+-----------+-' | | .-(34)-. | +-DECFLOAT--+------+------------------------------------------------+ | '-(16)-' | | .-(1)-------. | +-+-+-+-CHARACTER-+--+-----------+----------+--+----------------+-+-+ | | | '-CHAR------' '-(integer)-' | +-FOR BIT DATA---+ | | | | '-+-VARCHAR----------------+--(integer)-' +-FOR SBCS DATA--+ | | | | '-+-CHARACTER-+--VARYING-' '-FOR MIXED DATA-' | | | | '-CHAR------' | | | | .-(1M)-------------. | | | '-+-CLOB------------------------+--+------------------+---------' | | '-+-CHARACTER-+--LARGE OBJECT-' '-(integer-+---+-)-' | | '-CHAR------' +-K-+ | | +-M-+ | | '-G-' | | .-(1)-------. | +-+-GRAPHIC--+-----------+-------+----------------------------------+ | | '-(integer)-' | | | +-VARGRAPHIC--(integer)--------+ | | | .-(1M)-------------. | | | '-DBCLOB--+------------------+-' | | '-(integer-+---+-)-' | | +-K-+ | | +-M-+ | | '-G-' | | .-(1M)-------------. | +-+-BLOB----------------+--+------------------+---------------------+ | '-BINARY LARGE OBJECT-' '-(integer-+---+-)-' | | +-K-+ | | +-M-+ | | '-G-' | +-+-DATE---------------------+--------------------------------------+ | +-TIME---------------------+ | | | .-(6)-------. | | | '-TIMESTAMP--+-----------+-' | | '-(integer)-' | | .-SYSPROC.-. | '-+----------+--DB2SECURITYLABEL------------------------------------'
When casting character strings (other than CLOBs) to a character string with a different length, a warning (SQLSTATE 01004) is returned if truncation of other than trailing blanks occurs. When casting graphic character strings (other than DBCLOBs) to a graphic character string with a different length, a warning (SQLSTATE 01004) is returned if truncation of other than trailing blanks occurs. For BLOB, CLOB and DBCLOB cast operands, the warning is issued if any characters are truncated.
When casting an array, the target data type must be a user-defined array data type (SQLSTATE 42821). The data type of the elements of the array must be the same as the data type of the elements of the target array data type (SQLSTATE 42846). The cardinality of the array must be less than or equal to the maximum cardinality of the target array data type (SQLSTATE 2202F).
When numeric data is cast to character data, the result data type is a fixed-length character string. When character data is cast to numeric data, the result data type depends on the type of number specified. For example, if cast to integer, it becomes a large integer.
SELECT EMPNO, CAST(SALARY AS INTEGER) FROM EMPLOYEE
UPDATE PERSONNEL SET RETIRE_YEAR =?
WHERE AGE = CAST( ? AS T_AGE)
The first parameter is an untyped parameter marker that would have a data type of R_YEAR, although the application will use an integer for this parameter marker. This does not require the explicit CAST specification because it is an assignment.
The second parameter marker is a typed parameter marker that is cast as a distinct type T_AGE. This satisfies the requirement that the comparison must be performed with compatible data types. The application will use the source data type (which is SMALLINT) for processing this parameter marker.
Successful processing of this statement assumes that the SQL path includes the schema name of the schema (or schemas) where the two distinct types are defined.
CAST( ? AS VARCHAR(10000) FOR BIT DATA)
CREATE TYPE PHONELIST AS DECIMAL(10, 0) ARRAY[5]
CREATE TABLE EMP_PHONES
(ID INTEGER,
PHONENUMBER DECIMAL(10,0) )
The following
procedure returns an array with the phone numbers for the employee
with ID 1775. If there are more than five phone numbers
for this employee, an error is returned (SQLSTATE 2202F). CREATE PROCEDURE GET_PHONES(OUT EPHONES PHONELIST)
BEGIN
SELECT CAST(ARRAY_AGG(PHONENUMBER) AS PHONELIST)
INTO EPHONES
FROM EMP_PHONES
WHERE ID = 1775;
END