Updated on 2025-10-23 GMT+08:00

Explicit Type Conversion

Explicit type conversion mainly applies to the scenario where type conversion functions are called. For details about type conversion functions, see Type Conversion Functions.

The explicit type conversion syntax is as follows:

SELECT CAST(expression AS data_type);

expression indicates the expression to be converted, and data_type indicates the target data type. Table 1 lists the target types that are supported.

Table 1 Explicit cast conversion

Target Type

Data Type of Input Values (Type of Return Parameters)

SIGNED [INTEGER]

BIGINT

UNSIGNED [INTEGER]

BIGINT UNSIGNED

DATE

DATE

TIME

TIME

DATETIME

DATETIME

BINARY

TEXT

CHAR

TEXT

DECIMAL

NUMERIC

FLOAT

FLOAT4

DOUBLE

FLOAT8

Example:

-- Convert cast usage of SIGNED.
m_db=# SELECT CAST(9223372036854775808 AS SIGNED);
         cast         
----------------------
 -9223372036854775808
(1 row)

-- Boundary value processing.
m_db=# SELECT CAST(9223372036854775808.0 AS SIGNED);
WARNING:  Truncated incorrect DECIMAL value: '9223372036854775808.0'
CONTEXT:  referenced column: cast
        cast         
---------------------
 9223372036854775807
(1 row)

-- Convert cast usage of CHAR (modifiers-limited boundaries).
m_db=# SELECT CAST(123 AS CHAR(3));
 cast 
------
 123
(1 row)

Double-colon syntaxes, such as "SELECT 1::INT", can also be used for explicit conversion. The logic of double-colon explicit conversion is the same as the default logic of implicit conversion, and new data types in an M-compatible database support the double-colon syntaxes. You are advised not to use the conversion mode directly because the result may not meet the expectation.