An implicit cast is a cast that the database server can invoke automatically when it encounters data types that cannot be compared with built-in casts.
This type of cast enables the database server to automatically handle conversions between other data types.
1 Definition
To define an implicit cast, specify the IMPLICIT keyword in the CREATE CAST statement. For example, the following CREATE CAST statement specifies that the database server should automatically use the prcnt_to_char( ) function to convert from the CHAR data type to a distinct data type, percent:
Copy
CREATE IMPLICIT CAST (CHAR AS percent WITH char_to_prcnt);
This cast only supports automatic conversion from the CHAR data type to percent. For the database server to convert from percent to CHAR, you also need to define another implicit cast, as follows:
Copy
CREATE IMPLICIT CAST (percent AS CHAR WITH prcnt_to_char);
The database server automatically invokes the char_to_prcnt( ) function to evaluate the WHERE clause of the following SELECT statement:
Copy
SELECT commission FROM sales_rep WHERE commission > "25
2 Example
Copy
create function expcast_int_to_bool(i integer) returning boolean; if (i is null) then return null; elif (i != 0) then return 't'; else return 'f'; end if; end function; create explicit cast (integer as boolean with expcast_int_to_bool);
Copy
create table mytable ( id integer not null, isavailable boolean not null ); insert into mytable values(1, 't'); insert into mytable values(2, 'f'); select id from mytable where isavailable = cast(0 as boolean); select id from mytable where isavailable = cast(1 as boolean); select id from mytable where isavailable = cast(-1 as boolean); select id from mytable where isavailable = cast('t' as boolean); select id from mytable where isavailable = cast('f' as boolean);