X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id E045F4763B6 for ; Wed, 7 May 2003 20:59:24 -0400 (EDT) Received: from mail.vis-ken.com (cm61-15-221-199.hkcable.com.hk [61.15.221.199]) by postgresql.org (Postfix) with ESMTP id 8DEDB47636B for ; Wed, 7 May 2003 20:59:20 -0400 (EDT) Received: from home3 (home3.vis-ken.com [192.168.3.3]) by mail.vis-ken.com (Postfix) with SMTP id 20E71A4BE for ; Thu, 8 May 2003 07:56:35 +0800 (HKT) Message-ID: <000e01c314fd$208822e0$0303a8c0@home3> From: "Kenny Mok" To: Subject: Type coercion on column in a query Date: Thu, 8 May 2003 08:59:49 +0800 MIME-Version: 1.0 Content-Type: text/plain; charset="big5" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4922.1500 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4925.2800 X-Spam-Status: No, hits=-3.1 required=5.0 tests=BAYES_20 version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200305/271 X-Sequence-Number: 13436 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' testing=# testing=# SELECT testing-# to_number(data1) - to_number(data2) testing-# as result from test testing-# where id = 2; ERROR: Function 'to_number(varchar)' does not exist Unable to identify a function that satisfies the given argument types You may need to add explicit typecasts testing=# It seems that using the casting function and to_number function cannot casting the target columns into my requied type. However, when the argument given to these functions are literals, the result is like this : testing=# SELECT testing-# cast('1234' as numeric) - cast('34' as numeric) testing-# as result ; result -------- 1200 (1 row) testing=# SELECT testing-# to_number('1234') - to_number('34') testing-# as result ; ERROR: Function 'to_number(unknown)' does not exist Unable to identify a function that satisfies the given argument types You may need to add explicit typecasts testing=# The casting function works as my expected while the to_number function does not. So, how can I cast the column into the datatype I want during querying before shown in front of my users?? And any concept that I have mis-understood already ?? Thanks for your help in advance. ^_^