From: David Johnston <polobo@yahoo.com>
To: 'Carlos Mennens' <carlos.mennens@gmail.com>
To: 'PostgreSQL \(SQL\)' <pgsql-sql@postgresql.org>
Subject: Re: Unable To Alter Data Type
Date: Wed, 11 Jan 2012 19:13:55 -0500
Message-ID: <00be01ccd0bf$1340da50$39c28ef0$@yahoo.com> (raw)
In-Reply-To: <CAAQLLO5aXWj=Ss+9rKyohWcnb3Fnm2k_pAQu_pDfL=76VrbHzQ@mail.gmail.com>
References: <CAAQLLO5aXWj=Ss+9rKyohWcnb3Fnm2k_pAQu_pDfL=76VrbHzQ@mail.gmail.com>
-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Carlos Mennens
Sent: Wednesday, January 11, 2012 6:53 PM
To: PostgreSQL (SQL)
Subject: [SQL] Unable To Alter Data Type
Now I'm attempting to ALTER the field 'cust_zip' TYPE from character to
integer however I'm getting the following error:
----------------------------------------------------------------------------
--------
I think there is a rule in PostgreSQL that says that any field with "zip" in
the name MUST BE of a character type :)
You seriously do not want to turn this into an integer (big or otherwise) -
ZIP (POSTAL) CODES ARE NOT NUMERIC; THEY ARE TEXT!
If someone is telling you to make this change then tell them they are wrong
and whatever requirement are in place on the other end need to be modified -
not the table you indicate.
I do confirm, however, that a direct cast from character to bigint works and
thus you SHOULD be able to perform the TYPE alteration without adding the
USING (expression) clause. So either this is a bug OR, more likely,
character-to-bigint casting is not implicit (or an assignment cast, whatever
that is) and so you must resort to the USING clause - which does work.
[ ... ALTER COLUMN zip_code TYPE bigint USING (zip_code::bigint) ]
http://www.postgresql.org/docs/9.0/interactive/sql-altertable.html
This is on 9.0 for me.
However, I will say again, you DO NOT WANT TO ACTUALLY DO THIS!
The specific issue is that some US Postal Code begin with a zero ( 0 ) and
so whenever you want to the zip_code value you need to pad leading zeros if
the length is less than 5. Now consider that a full zip_code can be in 5+4
format with an embedded hyphen and you no longer can even store it as
numeric. If you deal with Canada (and maybe Mexico) at all then spaces and
letters become acceptable characters within the zip_code.
David J.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: polobo@yahoo.com, carlos.mennens@gmail.com
Subject: Re: Unable To Alter Data Type
In-Reply-To: <00be01ccd0bf$1340da50$39c28ef0$@yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox