CAST

Enables you to convert data of one type to another type. CAST can be used wherever a constant can be used. CAST is useful in specifying the exact data type for an argument. This is especially true for unary operators like '-' or functions with one operand like TO_CHAR or TO_DATE.

A value can only be CAST to a compatible data type, with the exception of NULL. NULL can be cast to any data type. CAST is not needed to convert a NULL literal to the desired target type.

The following conversions are supported:

  • Numeric value to numeric or BCD (Binary Coded Decimal)

  • NCHAR to NCHAR

  • CHAR string to BINARY string or DATE, TIME or TIMESTAMP

  • BINARY string to BINARY or CHAR string

  • DATE, TIME or TIMESTAMP to CHAR

SQL syntax

CAST
  ( {Expression | NULL} AS DataType )

Parameters

CAST has the parameters:

Parameter Description

Expression

Specifies the value to be converted.

AS DataType

Specifies the resulting data type.

Description

  • CAST to a domain name is not supported.

  • Casting a selected value may cause the SELECT statement to take more time and memory than a SELECT statement without a CAST expression.

Examples

INSERT INTO t1 VALUES(TO_CHAR(CAST(? AS REAL)));
SELECT CONCAT(x1, CAST (? AS CHAR(10))) FROM t1;	
SELECT * FROM t1 WHERE CAST (? AS INT)=CAST(? AS INT);