X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 80B144763A2 for ; Wed, 14 May 2003 12:32:13 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 87647-03 for ; Wed, 14 May 2003 12:32:11 -0400 (EDT) Received: from anchor-post-33.mail.demon.net (anchor-post-33.mail.demon.net [194.217.242.91]) by postgresql.org (Postfix) with ESMTP id 49490476348 for ; Wed, 14 May 2003 12:32:11 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-33.mail.demon.net with esmtp (Exim 3.35 #1) id 19FzAt-000Eyd-0X; Wed, 14 May 2003 17:32:15 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 3C5D816F38; Wed, 14 May 2003 17:32:14 +0100 (BST) Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id 9AA0816A50; Wed, 14 May 2003 17:32:13 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: Randall Lucas Subject: Re: Type coercion on column in a query Date: Wed, 14 May 2003 17:32:11 +0100 User-Agent: KMail/1.4.3 Cc: "Kenny Mok" , References: <8D873114-8626-11D7-8D9A-000A957653D6@tercent.net> In-Reply-To: <8D873114-8626-11D7-8D9A-000A957653D6@tercent.net> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200305141732.11386.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/285 X-Sequence-Number: 13450 On Wednesday 14 May 2003 5:10 pm, Randall Lucas wrote: > Along the same lines, is it possible to tell whether a column may be > cast to a given type (e.g. numeric) without throwing an error? > > Specifically, I have some values that will probably be numbers but > might have some free text or other gobbledygook in there. I want to > take the sum of all the numeric values, and maybe the average as well. > So what I envision is: > > table dirty_data ( > id int, > dirty_numbers text > ); > select sum( safe_numeric_cast(dirty_numbers) ) from dirty_data where > safe_numeric_cast(dirty_numbers) is not null; You'd have to write "safe_numeric_cast()" in plperl, or possibly pltcl (sor= ry,=20 I don't use TCL). There's no exception handling in Postgresql (nor will the= re=20 be in the next few months). Alternatively, you could conceivably write a bunch of is_numeric(), is_int(= )=20 etc functions and test first. --=20 Richard Huxton