Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:55:08+00:00 2026-06-13T16:55:08+00:00

I have a small table and a certain field contains the type character varying

  • 0

I have a small table and a certain field contains the type “character varying“. I’m trying to change it to “Integer” but it gives an error that casting is not possible.

Is there a way around this or should I just create another table and bring the records into it using a query.

The field contains only integer values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:55:09+00:00Added an answer on June 13, 2026 at 4:55 pm

    There is no implicit (automatic) cast from text or varchar to integer (i.e. you cannot pass a varchar to a function expecting integer or assign a varchar field to an integer one), so you must specify an explicit cast using ALTER TABLE … ALTER COLUMN … TYPE … USING:

    ALTER TABLE the_table ALTER COLUMN col_name TYPE integer USING (col_name::integer);
    

    Note that you may have whitespace in your text fields; in that case, use:

    ALTER TABLE the_table ALTER COLUMN col_name TYPE integer USING (trim(col_name)::integer);
    

    to strip white space before converting.

    This shoud’ve been obvious from an error message if the command was run in psql, but it’s possible PgAdmin-III isn’t showing you the full error. Here’s what happens if I test it in psql on PostgreSQL 9.2:

    => CREATE TABLE test( x varchar );
    CREATE TABLE
    => insert into test(x) values ('14'), (' 42  ');
    INSERT 0 2
    => ALTER TABLE test ALTER COLUMN x TYPE integer;
    ERROR:  column "x" cannot be cast automatically to type integer
    HINT:  Specify a USING expression to perform the conversion. 
    => ALTER TABLE test ALTER COLUMN x TYPE integer USING (trim(x)::integer);
    ALTER TABLE        
    

    Thanks @muistooshort for adding the USING link.

    See also this related question; it’s about Rails migrations, but the underlying cause is the same and the answer applies.

    If the error still occurs, then it may be related not to column values, but indexes over this column or column default values might fail typecast. Indexes need to be dropped before ALTER COLUMN and recreated after. Default values should be changed appropriately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used JQuery for a small animation work: a table #photos contains 9
I'm trying to create a small Web App to categorize certain type of YouTube
I have a small table of measurement units in Oracle (10.2.0.4). It's defined as
I am new to iphone.I have a small doubt (i.e),I have create a table
We have a table in which we store millions of small BLOBs (2-10kb). And
I have a LaTeX table that looks like this: \begin{table}[!ht] \centering \small \caption{ \bf{Caption}}
Just a small question regarding joins. I have a table with around 30 fields
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>
I have a small table (~30 rows) in my Postgres 9.0 database with an
I have a relatively small Table in a SQL database in my Android app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.