Home » Documentation » Using VoltDB


CAST() — explicitly converts an expression to the specified datatype.


CAST( expression AS datatype )


The CAST() function converts an expression to a specified datatype. Cases where casting is beneficial include when converting between numeric types (such as integer and float) or when converting a numeric value to a string.

All numeric datatypes can be used as the source and numeric or string datatypes can be the target. When converting from decimal values to integers, values are truncated. Where the runtime value cannot be converted (for example, the value exceeds the maximum allowable value of the target datatype) an error is thrown.

You cannot use VARBINARY or TIMESTAMP as either the target or the source datatype. Use the TO_TIMESTAMP(), FROM_UNIXTIME(), and EXTRACT() functions to convert to and from timestamp values.

The result of the CAST() function of a null value is the corresponding null in the target datatype.


The following example uses the CAST() function to ensure the result of an expression is also a floating point number and does not truncate the decimal portion.

SELECT contestant, CAST( (votes * 100) as FLOAT) / ? as percentage 
    FROM contest ORDER BY votes, contestant