CAST converts a value from one data type to another.

The following source data type conversion functions are supported for CAST

Source Can be converted to  
CHAR CHAR, LONG VARCHAR, VARCHAR, BIGINT, DECIMAL/NUMERIC, INTEGER, SMALLINT, DATE, TIME, TTIMESTAMP  
LONG VARCHAR CHAR, LONG VARCHAR, VARCHAR  
VARCHAR CHAR, LONG VARCHAR, VARCHAR, BIGINT, DECIMAL/NUMERIC, INTEGER, SMALLINT, DATE, TIME, TIMESTAP  
BIGINT CHAR, BIGINT, DECIMAL/NUMERIC, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT  
DECIMAL/NUMERIC CHAR, BIGINT, DECIMAL/NUMERIC, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT  
DOUBLE BIGINT, DECIMAL/NUMERIC, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT  
FLOAT BIGINT, DECIMAL/NUMERIC, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT  
INTEGER CHAR, BIGINT, DECIMAL/NUMERIC, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT \
REAL CHAR, BIGINT, DECIMAL/NUMERIC, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT  
SMALLINT CHAR, BIGINT, DECIMAL/NUMERIC, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT  
DATE CHAR, VARCHAR, DATE  
TIME CHAR, VARCHAR, TIME  
TIMESTAMP CHAR, VARCHAR, DATE, TIME, TIMESTAMP  

Syntax

CAST ( sourceValue AS targetType )

Arguments

  • sourceValue: A value to convert to targetType. sourceValue can be NULL.
  • targetType: The resulting data type.

Returns

The source value as a targetType data type.

Examples

SELECT CAST (salesTotal AS DECIMAL(5,2)) FROM MYSQL.inventory.orders;

VALUES CAST (1 AS SMALLINT)