---
title: "CREATE CAST"
url: "https://docs.yugabyte.com/stable/api/ysql/the-sql-language/statements/ddl_create_cast/"
---

# CREATE CAST

Use the CREATE CAST statement to create a cast.

## Synopsis

Use the CREATE CAST statement to create a cast.

## Syntax

- [![Grammar Icon](/icons/file-lines.svg)Grammar](#grammar-1)
- [![Diagram Icon](/icons/diagram.svg)Diagram](#diagram-1)

```output.ebnf
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\_function](#create-cast-with-function)[create\_cast\_without\_function](#create-cast-without-function)[create\_cast\_with\_inout](#create-cast-with-inout)

#### create\_cast\_with\_function

CREATECAST([cast\_signature](#cast-signature))WITHFUNCTION[subprogram\_name](/stable/api/ysql/syntax_resources/grammar_diagrams#subprogram-name)([subprogram\_signature](/stable/api/ysql/syntax_resources/grammar_diagrams#subprogram-signature))ASASSIGNMENTASIMPLICIT

#### create\_cast\_without\_function

CREATECAST([cast\_signature](#cast-signature))WITHOUTFUNCTIONASASSIGNMENTASIMPLICIT

#### create\_cast\_with\_inout

CREATECAST([cast\_signature](#cast-signature))WITHINOUTASASSIGNMENTASIMPLICIT

#### cast\_signature

[source\_type](/stable/api/ysql/syntax_resources/grammar_diagrams#source-type)AS[target\_type](/stable/api/ysql/syntax_resources/grammar_diagrams#target-type)

## Semantics

See the semantics of each option in the [PostgreSQL docs](https://www.postgresql.org/docs/15/sql-createcast.html "PostgreSQL docs").

## Examples

WITH FUNCTION example.

```plpgsql
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.

```plpgsql
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.

```plpgsql
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

- [DROP CAST](/stable/api/ysql/the-sql-language/statements/ddl_drop_cast "DROP CAST")
