Synopsis

Use the CREATE CAST statement to create a cast.

Syntax

create_cast ::= create_cast_with_function
                | create_cast_without_function
                | create_cast_with_inout

create_cast_with_function ::= CREATE CAST ( cast_signature ) WITH 
                              FUNCTION subprogram_name 
                              [ ( subprogram_signature ) ] 
                              [ AS ASSIGNMENT | AS IMPLICIT ]

create_cast_without_function ::= CREATE CAST ( cast_signature ) 
                                 WITHOUT FUNCTION 
                                 [ AS ASSIGNMENT | AS IMPLICIT ]

create_cast_with_inout ::= CREATE CAST ( cast_signature ) WITH INOUT 
                           [ AS ASSIGNMENT | AS IMPLICIT ]

cast_signature ::= source_type AS target_type

create_cast

create_cast_with_functioncreate_cast_without_functioncreate_cast_with_inout

create_cast_with_function

CREATECAST(cast_signature)WITHFUNCTIONsubprogram_name(subprogram_signature)ASASSIGNMENTASIMPLICIT

create_cast_without_function

CREATECAST(cast_signature)WITHOUTFUNCTIONASASSIGNMENTASIMPLICIT

create_cast_with_inout

CREATECAST(cast_signature)WITHINOUTASASSIGNMENTASIMPLICIT

cast_signature

source_typeAStarget_type

Semantics

See the semantics of each option in the [PostgreSQL docs][postgresql-docs-create-cast].

Examples

WITH FUNCTION example.

yugabyte=# CREATE FUNCTION sql_to_date(integer) RETURNS date AS $$
             SELECT $1::text::date
             $$ LANGUAGE SQL IMMUTABLE STRICT;
yugabyte=# CREATE CAST (integer AS date) WITH FUNCTION sql_to_date(integer) AS ASSIGNMENT;
yugabyte=# SELECT CAST (3 AS date);

WITHOUT FUNCTION example.

yugabyte=# CREATE TYPE myfloat4;
yugabyte=# CREATE FUNCTION myfloat4_in(cstring) RETURNS myfloat4
             LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'float4in';
yugabyte=# CREATE FUNCTION myfloat4_out(myfloat4) RETURNS cstring
             LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'float4out';
yugabyte=# CREATE TYPE myfloat4 (
             INPUT = myfloat4_in,
             OUTPUT = myfloat4_out,
             LIKE = float4
           );
yugabyte=# SELECT CAST('3.14'::myfloat4 AS float4);
yugabyte=# CREATE CAST (myfloat4 AS float4) WITHOUT FUNCTION;
yugabyte=# SELECT CAST('3.14'::myfloat4 AS float4);

WITH INOUT example.

yugabyte=# CREATE TYPE myint4;
yugabyte=# CREATE FUNCTION myint4_in(cstring) RETURNS myint4
             LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4in';
yugabyte=# CREATE FUNCTION myint4_out(myint4) RETURNS cstring
             LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4out';
yugabyte=# CREATE TYPE myint4 (
             INPUT = myint4_in,
             OUTPUT = myint4_out,
             LIKE = int4
           );
yugabyte=# SELECT CAST('2'::myint4 AS int4);
yugabyte=# CREATE CAST (myint4 AS int4) WITH INOUT;
yugabyte=# SELECT CAST('2'::myint4 AS int4);

See also