Unix Technical Forum

Reg: Changing Column type

This is a discussion on Reg: Changing Column type within the pgsql Admins forums, part of the PostgreSQL category; --> Hi, When i am tring to change the column type from character/Varchar to "int" i am getting error like, ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 06:29 AM
sandhya
 
Posts: n/a
Default Reg: Changing Column type

Hi,

When i am tring to change the column type from character/Varchar to "int" i am getting error like,

ERROR: column "fname" cannot be cast to type "int4"

I can able to change the column type to "int " if the old datatype is double etc.
Even a "int" datatype also i can able to conver to "char"
and why not viceversa....Please tell me where the problem is.
Suggest me what to do to convert a "char" datatype to "int".

Thanks&Regards,
Sandhya





Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 06:29 AM
Tom Lane
 
Posts: n/a
Default Re: Reg: Changing Column type

"sandhya" <sandhyar@amiindia.co.in> writes:
> When i am tring to change the column type from character/Varchar to =
> "int" i am getting error like,
> ERROR: column "fname" cannot be cast to type "int4"


If this is a pre-8.0 PG version, try casting to text and then int4.

If 8.0 or later, it should work as is --- would you show us exactly
what you did?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 06:29 AM
Bruno Wolff III
 
Posts: n/a
Default Re: Reg: Changing Column type

On Tue, Nov 15, 2005 at 17:22:16 +0530,
sandhya <sandhyar@amiindia.co.in> wrote:
> Hi,
>
> When i am tring to change the column type from character/Varchar to "int" i am getting error like,
>
> ERROR: column "fname" cannot be cast to type "int4"
>
> I can able to change the column type to "int " if the old datatype is double etc.
> Even a "int" datatype also i can able to conver to "char"
> and why not viceversa....Please tell me where the problem is.
> Suggest me what to do to convert a "char" datatype to "int".


One way to do this is to add a new int column. Use a query to insert data into
the new column using to_number allow with whatever you need to handle strings
that aren't numbers.
Then delete the old column.
Then rename the new column to be the original name.

In at least 8.1 there is a USING clause which allows you to specify a
conversion function which lets you do the above in one step.
I don't know when that feature was added though.

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:50 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com