Re: can't cast char to in

From: Richard Huxton <dev(at)archonet(dot)com>
To: blackwater dev <blackwaterdev(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: can't cast char to in
Date: 2008-04-22 17:09:03
Message-ID: 480E1BAF.40108@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

blackwater dev wrote:
> I have a table with a mileage column that is a character varying (please
> don't ask why :).

Why? :-)

> I need to do a query where mileage > 500
>
> select * from cars where mileage>500
>
> So I need to cast it but everything I try throws an error such as :
>
> ERROR: invalid input syntax for integer: "+"

Once you've cleaned your data, I would do one of two things:

1. Add a constraint to restrict the values the mileage column will accept:
ALTER TABLE cars ADD CONSTRAINT valid_mileage
CHECK (mileage ~ '^[+]?[0-9]+$');

2. You can alter the type on-the-fly too:
ALTER TABLE mileage_test ALTER COLUMN mileage TYPE integer
USING (mileage::int);

Note that you'll need to remove the constraint from #1 if you've applied
that.

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mary Ellen Fitzpatrick 2008-04-22 17:13:45 Can not restart postgres: Panic could not locate a valid checkpoint record
Previous Message Vanole, Mike 2008-04-22 17:04:27 Best approach for large table maintenance