Materialize Logo

CAST Function and Operator

The cast function and operator return a value converted to the specified type.

Signatures

CAST ( val AS type )
val :: type
Parameter Type Description
val Any The value you want to convert.
type Typename The return value’s type.

The following special syntax is permitted if val is a string literal:

type val

Return value

cast returns the value with the type specified by the type parameter.

Details

Valid casts

Source type Return type
int bool
int float
int decimal
int text
float int
float decimal1
float text
decimal int
decimal float
decimal text
date timestamp
date timestamptz
date text
time interval
time text
interval time
interval text

1 Casting a float to a decimal can yield an imprecise result due to the floating point arithmetic involved in the conversion.

Examples

SELECT INT '4';
 ?column?
----------
         4

SELECT CAST (CAST (100.21 AS decimal(10, 2)) AS float) AS dec_to_float;
 dec_to_float
--------------
       100.21

SELECT 100.21::decimal(10, 2)::float AS dec_to_float;
 dec_to_float
--------------
       100.21