Book Contents

Book Index

Next Topic

Home

Data Type Coercion Operators (CAST)

Type coercion (casting) passes an expression value to an input conversion routine for a specified data type, resulting in a constant of the indicated type.

Syntax

CAST ( expression AS data-type )

expression::data-type

data-type 'string'

Semantics

 

expression

is an expression of any type

data-type

converts the value of expression to one of the following data types:

BOOLEAN

CHARACTER (CHAR)

CHARACTER VARYING (VARCHAR)

Date/Time Types

DOUBLE PRECISION (FLOAT)

INTEGER (BIGINT)

Notes

Examples

=> SELECT CAST((2 + 2) AS VARCHAR);

varchar

---------

4

(1 row)

=> SELECT (2 + 2)::VARCHAR;

varchar

---------

4

(1 row)

 

=> SELECT '2.2' + 2;

ERROR: invalid input syntax for integer: "2.2"

 

=> SELECT FLOAT '2.2' + 2;

?column?

----------

4.2

(1 row)