X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C2D6247646B for ; Wed, 14 May 2003 11:38:42 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 68307-03 for ; Wed, 14 May 2003 11:38:38 -0400 (EDT) Received: from lubitsch.akademie.de (ns.akademie.de [62.165.4.3]) by postgresql.org (Postfix) with ESMTP id CFC2047645A for ; Wed, 14 May 2003 11:37:02 -0400 (EDT) Received: from [62.165.4.177] (helo=kidman.akademie1.de) by lubitsch.akademie.de with esmtp (Exim 3.33 #2) id 19FyJX-0003du-00 for pgsql-sql@postgresql.org; Wed, 14 May 2003 17:37:07 +0200 Content-Type: text/plain; charset="big5" From: Ian Barwick To: Subject: Re: Type coercion on column in a query Date: Wed, 14 May 2003 17:35:18 +0200 User-Agent: KMail/1.4.3 References: <000e01c314fd$208822e0$0303a8c0@home3> In-Reply-To: <000e01c314fd$208822e0$0303a8c0@home3> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: <200305141734.45558.barwick@gmx.net> X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/280 X-Sequence-Number: 13445 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@gmx.net