Skip to main content
Version: 2.0.0 (latest)

3.12. Casts

Casts are objects used to convert values between two different data types. PostgreSQL implements several type casts, but the user has the freedom to create custom casts to provide, for instance, the conversion between user-defined types and built-in data types and vice versa. Casts are simple objects in their configuration since the hard work is done by the cast function itself. The editing dialog of this kind of object is detailed as follows:

ch3.cast-dialog.general123456789

General
#FieldGroup boxDescription
1Cast type

Defines the cast mode.

Explicit: the cast must be explicitly called to perform the value conversion; for instance, the query SELECT CAST(10 AS float8) forces the conversion of the integer 10 to a float8 value.

Implicit: the cast is implicitly done, as in SELECT 2 + 10.25, where the parser converts the integer 2 to a numeric value and performs the sum.

Assignment: the cast is done when assigning a value to a column. Given the table tableA(id text), running INSERT INTO tableA(id) VALUES (20) converts 20 to a string and assigns it to the column id.

2Input / OutputCast type

Converts the value using the output function of the source data type and passes the resulting string to the input function of the target type, instead of using a cast function (WITH INOUT).

3Cast function

The function that performs the value conversion. It must have the signature typeB function(typeA, integer, boolean) to convert from typeA to typeB; the second parameter (the type modifier) and the third one (whether the cast is explicit) are optional. Leave it empty when Input / Output is checked.

4TypeSource data type

The data type. Besides the built-in types, the list also holds the user-defined types, domains and other types of the model.

5DimensionSource data type

Number of dimensions when the type is an array (type[]). 0 means that the type is not an array.

6TypeTarget data type

The data type. Besides the built-in types, the list also holds the user-defined types, domains and other types of the model.

7DimensionTarget data type

Number of dimensions when the type is an array (type[]). 0 means that the type is not an array.

8FormatSource data type

Read-only preview of the data type as it is written in the SQL code, with length, precision, dimension and other modifiers applied.

9FormatTarget data type

Read-only preview of the data type as it is written in the SQL code, with length, precision, dimension and other modifiers applied.

References​

Cast DDL
https://www.postgresql.org/docs/current/static/sql-createcast.html