Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VPG8k-00018k-C0 for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Sep 2013 18:18:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VPG8j-000661-Nk for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Sep 2013 18:18:13 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VPDP2-0004dA-Dg for pgsql-bugs@postgresql.org; Thu, 26 Sep 2013 15:22:52 +0000 Received: from mahout.postgresql.org ([2001:4800:7903:4::123]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VPDOw-00014l-0G for pgsql-bugs@postgresql.org; Thu, 26 Sep 2013 15:22:51 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VPDOv-0002H6-GA for pgsql-bugs@postgresql.org; Thu, 26 Sep 2013 15:22:45 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.80) (envelope-from ) id 1VPDOq-0002Sx-5f for pgsql-bugs@postgresql.org; Thu, 26 Sep 2013 15:22:40 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: BUG #8471: subquery where not being applied until outer query To: pgsql-bugs@postgresql.org From: dnrickner@taylor.edu Message-Id: Date: Thu, 26 Sep 2013 15:22:40 +0000 X-Pg-Spam-Score: -1.6 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 8471 Logged by: Dan Rickner Email address: dnrickner@taylor.edu PostgreSQL version: 9.2.4 Operating system: CentOS Description: Our ERP stores student GPA values as a text string. I am trying to select only valid gpa values (a number between 0 and 4.0). I have a function called numeric that returns a bool if the value can be converted to a number. When I run my query I get errors about values that should not be considered in my outer where statement. The inner where is supposed to filter out the bad data values: -- function to return if a character string can be converted to a number create or replace function isnumeric(varchar) returns boolean as $$ declare x numeric; begin x = $1::numeric; return true; exception when others then return false; end $$ language plpgsql immutable; -- test table create table tbl ( id serial not null, gpa varchar(6) null ); -- insert bad data insert into tbl (gpa) values ('A'), ('2.0'), ('12.5'), ('3.45'), (''), ('-'), ('-2.3'), ('-5'); select * from ( -- this subquery returrns only numeric values select a.id, trunc(a.gpa::numeric, 2) as gpa from ( select id, gpa, isnumeric(gpa) as num from tbl ) as a where a.num = true ) as b -- filter the numeric values to the 4.0 range where b.gpa between 0.0 and 4.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs