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.

CAST

Description

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.

Inputs
CAST(value AS newtype) 

value—The value that needs converting.

newtype—The new data type to convert to.

Examples
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 
Notes

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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.