Re: How to convert integer to boolean in insert

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to convert integer to boolean in insert
Date: 2009-04-05 12:19:14
Message-ID: gra7k0$eam$1@ger.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

JiangMiao wrote on 05.04.2009 13:14:
> for table
> foo
> banned:boolean
>
> When try run 'insert into foo(banned) values(0)'
> It returns
> ERROR: column "banned" is of type boolean but expression is of type
> integer
> LINE 1: insert into foo(banned) values(0)
>
> and I found a way to add the cast
> insert into foo(banned) values(0::boolean)
> but I have a big table which dump from mysqldump and covert by
> mysql2pgsql. all of boolean relation values is 0 instead of '0' or
> FALSE.
>
> Is there any way to make pgsql implicit cast the integer to boolean?

If that is a one-time thing, why not create the table with banned as an integer
column and another boolean column (if your INSERTs are properly listing the
column names), then after the import update the boolean to the casted integer
value, drop the integer and rename the boolean?

Something like:

CREATE TABLE foo (banned integer, banned_b boolean);

-- run your inserts

update foo set banned_b =
case banned
when 0 then false
else true
end;

alter table foo drop column banned;
alter table foo rename column banned_b to banned;

Thomas

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2009-04-05 12:36:12 Re: How to convert integer to boolean in insert
Previous Message JiangMiao 2009-04-05 11:14:37 How to convert integer to boolean in insert