Function Reference

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;
result
43

Rounding when casting to INTEGER

PostgreSQL 17.5
SELECT CAST(12005.6 AS INTEGER) AS result;
result
12006

Short form with ::

PostgreSQL 17.5
SELECT '42'::INTEGER * 2 AS result;
result
84

Details

TypeResult
INTEGER, BIGINTA whole number. A numeric fraction is rounded away from zero (2.5 → 3), a double precision one to even (2.5 → 2). A string must contain a whole number: '12abc' and '3.7' cause an error.
NUMERIC[(p, s)]An exact number: s is the number of decimal places, extra ones are rounded; a number that does not fit into p digits is an error.
DOUBLE PRECISIONA floating-point number with about 15 significant digits.
TEXT, VARCHAR(n)A string. VARCHAR(n) silently cuts the value to n characters.
DATEA date; a string must contain an existing date, the time is dropped.
TIMESTAMPA date with time, without a time zone.
BOOLEANA logical value; from strings it accepts 'true', 'false', 'yes', 'no', '1', '0'.

Instead of CAST(value AS type), you can write value::type.

See also

Where to learn