Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 945771337B6C for ; Fri, 28 Jan 2011 23:47:02 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 49179-10 for ; Sat, 29 Jan 2011 03:46:53 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtpcorp.com (smtpcorp.com [174.143.60.32]) by mail.postgresql.org (Postfix) with ESMTP id D3F771337B5E for ; Fri, 28 Jan 2011 23:46:50 -0400 (AST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpcorp.com; s=main; h=Content-Transfer-Encoding:Content-Type:Mime-Version:References:Message-ID:Date:Subject:From:To; bh=KksSrvZ5zHUv3r0Nm1me3MZz8lhbIOOf5CTLj7DJA44=; b=oVpbzUtcoyep/6KIalv7tO4iDTf2g0lreKcMmrdPcM5LjBCMAq73N3pBEBnkHy6KoD/F9lKd9ZBKdPR/t4MgXQ3/6+SKvfE6gjQcV/yZbRcmFVHwZxLkwJKehC/IoMfA; Received: from ip-118-90-138-211.xdsl.xnet.co.nz ([118.90.138.211] helo=gonzo) by smtpcorp.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.70) id 1Pj1m0-0001gP-Jn; mid 144 for pgsql-sql@postgresql.org; Sat, 29 Jan 2011 03:46:53 +0000 Received: from news by gonzo with local (Exim 4.72) (envelope-from ) id 1Pj1lm-0003ZI-0O for pgsql-sql@postgresql.org; Sat, 29 Jan 2011 16:46:38 +1300 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: something simple but I can't Date: 29 Jan 2011 03:46:37 GMT Organization: Dis (not Dat) Organisation Lines: 44 Message-ID: References: <201101281716.04534.johnf@jfcomputer.com> NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reversiblemaps.ath.cx 1296272797 13718 127.0.0.1 (29 Jan 2011 03:46:37 GMT) X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Smtpcorp-Track: 21627127.1.63141963 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201101/115 X-Sequence-Number: 35062 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' ; -- ⚂⚃ 100% natural