Conversion Functions

PostgreSQL includes a number of conversion functions. These are used to convert from one data type to another and to format specific output styles.



The CAST function can be used to convert from one data type to another. Generally speaking, CAST is a fairly generic and easy-to-use function that makes most data-type conversions easy.

CAST(value AS newtype) 

value—The value that needs converting.

newtype—The new data type to convert to.

CAST('57' as INT)) → 57 
CAST(57 as CHAR) → '57' 
CAST(57 as NUMERIC(4,2)) → 57.00 
CAST('05-23-87' as DATE)) → 1987-05-23 

An additional way to perform type conversion is to separate the value and the desired data type with double colons (::).

Then ...

Get PostgreSQL Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.