Pavel Stehule wrote:
> create or replace function int2bool (integer) returns boolean as '
> select case when $1=1 then ''t''::boolean else ''f''::boolean end;
> ' language sql;
I'd do it slightly differently, if only to cater to the principle of least surprise:
create or replace function int2bool (integer) returns boolean as '
select case when $1=0 then false else true end;
' language sql
That way, 0 maps to false, any non-zero value becomes true.
> create or replace function bool2int (boolean) returns integer as '
> select case when $1 then 0 else 1 end; ' language sql;
And that's back-to-front ;)
create or replace function bool2int (boolean) returns integer as '
select case when $1 then 1 else 0 end;
' language sql
Thanks for the example of the use of casts.
Alex Satrapa