skip to main content
Functions : Scalar functions : CAST
 

CAST

Syntax 

CAST (value_exp AS data_type)

Description

This function returns the value specified by value_exp converted to the specified data_type, where data_type is one of the following keywords:
BIGINT
BINARY
BIT
CHAR
DECIMAL
DATE
DOUBLE
FLOAT
INTEGER
LONGVARBINARY
LONGVARCHAR
NUMERIC
REAL
SMALLINT
TIME
TIMESTAMP
TINYINT
VARBINARY
VARCHAR
WCHAR
WVARCHAR
WLONGVARCHAR
The following table defines the precision, length and scale of the resulting expression.
Table 15: Length, precision, and scale keywords of the CAST function 
Keyword
Length
Precision
Scale
BIGINT
8
20
0
BINARY
256
255
0
BIT
256
1
0
CHAR
1
255
0
DATE
6
10
0
DOUBLE
8
15
0
FLOAT
8
15
0
INTEGER
4
10
0
LONGVARBINARY
1000000
1000000
0
LONGVARCHAR
1000000
1000000
0
NUMERIC
32
34
5
REAL
2
7
0
SMALLINT
4
5
0
TIME
6
8
0
TIMESTAMP
16
 
0
TINYINT
1
3
0
VARBINARY
1024
2048
0
VARCHAR
1024
2048
0
WCHAR
255
255
0
WLONGVARCHAR
2000000
1000000
0
WVARCHAR
2048
1024
0

Example

SELECT empno, CAST(empno AS VARCHAR) FROM emp
SELECT empno, CAST(empno AS SMALLINT) FROM emp