Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id C1D2C1337B4F for ; Sat, 29 Jan 2011 11:14:34 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 75690-02 for ; Sat, 29 Jan 2011 15:14:27 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from newserver.jfcomputer.com (unknown [76.14.161.106]) by mail.postgresql.org (Postfix) with ESMTP id 3A1F31337B6C for ; Sat, 29 Jan 2011 11:14:27 -0400 (AST) Received: from localhost (unknown [127.0.0.1]) by newserver.jfcomputer.com (Postfix) with ESMTP id 68E421234B for ; Sat, 29 Jan 2011 15:14:25 +0000 (UTC) X-Virus-Scanned: amavisd-new at site Received: from newserver.jfcomputer.com ([127.0.0.1]) by localhost (linux-jfp8.site [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PohpCZe7Lsjt for ; Sat, 29 Jan 2011 07:14:23 -0800 (PST) Received: from linux-desktop.localnet (unknown [192.168.1.108]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johnf@jfcomputer.com) by newserver.jfcomputer.com (Postfix) with ESMTPSA id E588212239 for ; Sat, 29 Jan 2011 07:14:22 -0800 (PST) From: John Fabiani Organization: J.F. Computer Solutions To: pgsql-sql@postgresql.org Subject: Re: something simple but I can't Date: Sat, 29 Jan 2011 07:14:22 -0800 User-Agent: KMail/1.13.5 (Linux/2.6.34.7-0.5-desktop; KDE/4.4.4; i686; ; ) References: <201101281716.04534.johnf@jfcomputer.com> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <201101290714.22366.johnf@jfcomputer.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.107 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793 X-Spam-Level: X-Archive-Number: 201101/116 X-Sequence-Number: 35063 On Friday, January 28, 2011 07:46:37 pm Jasen Betts wrote: > On 2011-01-29, John Fabiani wrote: > > Hi guys, > > I trying to return a 0.00 from a function it there are no records found > > else return the amount. > > > > select sum(aropen_paid) into _paidamt FROM public.aropen where > > > > aropen_applyto is not null and (aropen_applyto = $1) ; > > > > IF (FOUND) THEN > > > > RETURN _paidamt ; > > > > END IF; > > > > RETURN 0.00 ; > > > > But all I get is either a blank or the amount paid. What am I doing > > wrong??? Johnf > > how many rows does the query return when no rows match the where? > It returns 1 that looks like ( NULL ). > it return 1 row, which is more than zero thus FOUND is TRUE. > > you can fix your function by changing the IF to > > IF _paidamt IS NOT NULL > > > but if you change the sum to > > coalesce(sum(aropen_paid),0.00) > > you can do the task more simply like this: > > create or replace function danmeans_getpayments(text) > returns numeric as > $BODY$ > select coalesce(sum(aropen_paid),0.00) FROM public.aropen where > aropen_applyto is not null and (aropen_applyto = $1) ; > $BODY$ > LANGUAGE 'sql' ; Thanks to all who replied. All of your suggestions would work and it should have been obvious but it wasn't. Johnf