X-Original-To: pgsql-hackers-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4481AD806D; Mon, 29 Aug 2005 20:39:50 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 35919-04; Mon, 29 Aug 2005 23:39:48 +0000 (GMT) Received: from keymaster.look.ca (delta1.look.ca [207.136.80.9]) by svr1.postgresql.org (Postfix) with ESMTP id 49D9FD8047; Mon, 29 Aug 2005 20:39:47 -0300 (ADT) Received: from 216-154-12-204.dsl.look.ca ([216.154.12.204]) by keymaster.look.ca with esmtp (Exim 4.20) id 1E9tED-0007p4-SK; Mon, 29 Aug 2005 23:39:49 +0000 From: Rod Taylor To: "Marc G. Fournier" Cc: pgsql-hackers@postgresql.org In-Reply-To: <20050829201253.X1044@ganymede.hub.org> References: <20050829201253.X1044@ganymede.hub.org> Date: Mon, 29 Aug 2005 19:33:31 -0400 Message-Id: <1125358411.13034.103.camel@home> Mime-Version: 1.0 X-Mailer: Evolution 2.2.3 FreeBSD GNOME Team Port X-SA-Exim-Mail-From: pg@rbt.ca Subject: Re: ALTER TABLE ( smallinto -> boolean ) ... Content-Type: text/plain Content-Transfer-Encoding: 7bit X-SA-Exim-Version: 3.1 (built Tue Feb 24 05:09:27 GMT 2004) X-SA-Exim-Scanned: Yes X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.036 required=5 tests=[AWL=-0.014, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200508/1076 X-Sequence-Number: 72271 On Mon, 2005-08-29 at 20:15 -0300, Marc G. Fournier wrote: > I have a table with several 'smallint' fields that I'd like to convert to > booleean ... the data in each is either 0 or 1, and: > > # ALTER TABLE table ALTER COLUMN field1 type boolean; > ERROR: column "field1" cannot be cast to type "pg_catalog.bool" > > Should this not work? If not, is there a way to do it so that it will, > without having to reload the whole table? development=# select '0'::smallint::boolean; ERROR: cannot cast type smallint to boolean You were casting an unknown to boolean. Anyway, USING is what you're looking for: ALTER TABLE table ALTER COLUMN field1 TYPE boolean USING CASE WHEN field1 = 0 THEN FALSE WHEN field1 = 1 THEN TRUE ELSE NULL END; --