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.

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 string
float int
float decimal1
float string
decimal int
decimal float
decimal string
date timestamp
date timestamptz
date string

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

Examples 

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