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
toNCHAR
-
CHAR
string toBINARY
string orDATE
,TIME
orTIMESTAMP
-
BINARY
string toBINARY
orCHAR
string -
DATE
,TIME
orTIMESTAMP
toCHAR
SQL syntax
CAST ( {Expression
| NULL} ASDataType
)
Parameters
CAST
has the parameters:
Parameter | Description |
---|---|
|
Specifies the value to be converted. |
|
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 aSELECT
statement without aCAST
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);