CREATE CAST

Synopsis

Use the CREATE CAST statement to create a new cast.

Syntax

  1. create_cast ::= create_cast_with_function
  2. | create_cast_without_function
  3. | create_cast_with_inout
  4. create_cast_with_function ::= CREATE CAST ( cast_signature ) WITH
  5. FUNCTION function_name
  6. [ ( function_signature ) ]
  7. [ AS ASSIGNMENT | AS IMPLICIT ]
  8. create_cast_without_function ::= CREATE CAST ( cast_signature )
  9. WITHOUT FUNCTION
  10. [ AS ASSIGNMENT | AS IMPLICIT ]
  11. create_cast_with_inout ::= CREATE CAST ( cast_signature ) WITH INOUT
  12. [ AS ASSIGNMENT | AS IMPLICIT ]
  13. cast_signature ::= source_type AS target_type

create_cast

CREATE CAST - 图1

create_cast_with_function

CREATE CAST - 图2

create_cast_without_function

CREATE CAST - 图3

create_cast_with_inout

CREATE CAST - 图4

cast_signature

CREATE CAST - 图5

Semantics

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

Examples

WITH FUNCTION example.

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

WITHOUT FUNCTION example.

  1. yugabyte=# CREATE TYPE myfloat4;
  2. yugabyte=# CREATE FUNCTION myfloat4_in(cstring) RETURNS myfloat4
  3. LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'float4in';
  4. yugabyte=# CREATE FUNCTION myfloat4_out(myfloat4) RETURNS cstring
  5. LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'float4out';
  6. yugabyte=# CREATE TYPE myfloat4 (
  7. INPUT = myfloat4_in,
  8. OUTPUT = myfloat4_out,
  9. LIKE = float4
  10. );
  11. yugabyte=# SELECT CAST('3.14'::myfloat4 AS float4);
  12. yugabyte=# CREATE CAST (myfloat4 AS float4) WITHOUT FUNCTION;
  13. yugabyte=# SELECT CAST('3.14'::myfloat4 AS float4);

WITH INOUT example.

  1. yugabyte=# CREATE TYPE myint4;
  2. yugabyte=# CREATE FUNCTION myint4_in(cstring) RETURNS myint4
  3. LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4in';
  4. yugabyte=# CREATE FUNCTION myint4_out(myint4) RETURNS cstring
  5. LANGUAGE internal IMMUTABLE STRICT PARALLEL SAFE AS 'int4out';
  6. yugabyte=# CREATE TYPE myint4 (
  7. INPUT = myint4_in,
  8. OUTPUT = myint4_out,
  9. LIKE = int4
  10. );
  11. yugabyte=# SELECT CAST('2'::myint4 AS int4);
  12. yugabyte=# CREATE CAST (myint4 AS int4) WITH INOUT;
  13. yugabyte=# SELECT CAST('2'::myint4 AS int4);

See also