argh... can't cast varchar to int - Mailing list pgsql-general

From Matthew Nuzum
Subject argh... can't cast varchar to int
Date
Msg-id 1038191293.3524.119.camel@localhost.localdomain
Whole thread Raw
Responses Re: argh... can't cast varchar to int  (Oliver Elphick <olly@lfix.co.uk>)
List pgsql-general
I have a table that has (among other things) two values, one named
ftr_type and the other ftr_data.

Depending on the value of ftr_type, ftr_data may contain the value of a
primary key in another table.  Because sometimes there is another table
and sometimes there isn't, ftr_data is a varchar.

I'm writing some SQL functions that will help me maintain referential
integrity, but they won't let me use the value in ftr_data as a
reference to the other table.  It says:

Cannot cast type 'character varying' to 'integer'

Here's what I've tried:
select CAST(ftr_data as int4) from pages where pageid = 2783;
ERROR:  Cannot cast type 'character varying' to 'integer'

select ftr_data::int4 from pages where pageid = 2783;
ERROR:  Cannot cast type 'character varying' to 'integer'

select to_num(ftr_data) from pages where pageid = 2783;
ERROR:  Function 'to_num(varchar)' does not exist
        Unable to identify a function that satisfies the given argument
types
        You may need to add explicit typecasts

Just for reference, here's what the data in ftr_data looks like in this
case:
select ftr_data from pages where pageid = 2783;
 ftr_data
----------
 22

Also for reference, this doesn't work:
select CAST('22'::varchar as int4);
ERROR:  Cannot cast type 'character varying' to 'integer'

Can anyone suggest a way that I can do this in my pl/pgsql function?
Thanks,
Matthew Nuzum



pgsql-general by date:

Previous
From: Jean-Christian Imbeault
Date:
Subject: Re: Calculated fileds in pg
Next
From: Oliver Elphick
Date:
Subject: Re: argh... can't cast varchar to int