CAST()

Documentation

VoltDB Home » Documentation » Using VoltDB

CAST()

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

Synopsis

CAST( expression AS datatype )

Description

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. You can also cast from a TIMESTAMP to a VARCHAR or from a VARCHAR to a TIMESTAMP, assuming the text string is formatted as YYYY-MM-DD or YYYY-MM-DD HH:MM:SS.nnnnnnn. 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 as either the target or the source datatype. To convert between numeric and TIMESTAMP values, use the TO_TIMESTAMP(), FROM_UNIXTIME(), and EXTRACT() functions.

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

Example

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;