Re: Type coercion on column in a query

From: Ian Barwick <barwick(at)gmx(dot)net>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Type coercion on column in a query
Date: 2003-05-14 15:35:18
Message-ID: 200305141734.45558.barwick@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thursday 08 May 2003 02:59, Kenny Mok wrote:
> Dear all,
>
> I am just a novice in SQL and also in PostgreSQL. I have encountered some
> difficulties in developing a website using PostgreSQL as my backend
> database.
>
> My situation is, I have such a table "test" :
>
> testing=# SELECT * from test ;
> id | data1 | data2
> ----+------------+--------
> 1 | 2003-5-6 | 3 days
> 2 | 1234 | 34
> (2 rows)
>
> where columns data1 and data 2 are with data types varchar, where all my
> data is stored into it.
>
> What I want to do is to extracts the data from this database and casting it
> before shown in front of my client. So, I do the following queries :
>
> testing=# SELECT
> testing-# cast(data1 as numeric) - cast(data2 as numeric)
> testing-# as result from test
> testing-# where id = 2;
> ERROR: Cannot cast type 'character varying' to 'numeric'

There is no cast between varchar and numeric / int; you will need to
go via text, e.g.

SELECT data1::text::numeric

or

SELECT cast(cast(data1 as text) as numeric)

Ian Barwick
barwick(at)gmx(dot)net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Randall Lucas 2003-05-14 15:43:41 Re: [SQL] insert problem with special characters
Previous Message Richard Huxton 2003-05-14 15:34:28 Re: Questions for experts