功能和类型
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);
示范
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);
这个演示可以通过DB Access运行—它正确地返回第一个和最后一个SELECT语句的ID 2和其他三个SELECT语句的ID 1。我还没有正式证明,当整数或布尔文字替换为“?”时,它仍然有效。不过,它至少有适度的可能奏效。