CAST
Converts a value to the specified data type.
PostgreSQL 17.5
CAST(value AS type)Parameters
- value
- Value to convert
- typedata type
- Target data type, for example INTEGER, NUMERIC, DATE or TEXT
Return value
A value of the given type; a failed conversion is a query error
Examples
String to a number
PostgreSQL 17.5
SELECT CAST('42' AS INTEGER) + 1 AS result;Rounding when casting to INTEGER
PostgreSQL 17.5
SELECT CAST(12005.6 AS INTEGER) AS result;Short form with ::
PostgreSQL 17.5
SELECT '42'::INTEGER * 2 AS result;Details
Instead of CAST(value AS type), you can write value::type.