PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL CAST() Function



The PostgreSQL CAST() function converts a value from one datatype to another datatype.

Syntax

CAST(value AS type)

Parameters

value Required. Specify the value to convert.
type Required. Specify the datatype to convert to.

Return Value

Returns the converted value.

Convert to DATE

The CAST() function can be used to convert a value to a DATE type. For example - in the example below, '2018-08-18' is converted to DATE datatype.

SELECT CAST('2018-08-18' AS DATE);
Result: '2018-08-31'

Convert to TIMESTAMP

The CAST() function can be used to convert a value to a TIMESTAMP type. For example - in the example below, '2018-08-18 10:38:42' is converted to TIMESTAMP datatype.

SELECT CAST('2018-08-18 10:38:42' AS TIMESTAMP);
Result: '2018-08-18 10:38:42'

Convert to TIME

The CAST() function can be used to convert a value to a TIME type. For example - in the example below, '10:38:42' is converted to TIME datatype.

SELECT CAST('10:38:42' AS TIME);
Result: '10:38:42'

Convert to DECIMAL

The CAST() function can be used to convert a value to a DECIMAL type. For example - in the example below, '123.456' is converted to DECIMAL datatype.

SELECT CAST('123.456' AS DECIMAL(5, 2));
Result: 123.46

Convert to VARCHAR

The CAST() function can be used to convert a value to a VARCHAR type. For example - in the example below, 123 is converted to VARCHAR datatype.

SELECT CAST(123 AS VARCHAR);
Result: '123'

Convert to BOOLEAN

The CAST() function can be used to convert a value to a BOOLEAN type. For example - in the example below, 1 is converted to BOOLEAN datatype.

SELECT CAST(1 AS BOOLEAN);
Result: 't'

❮ PostgreSQL Functions