Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id AA9A4475BF9 for ; Wed, 16 Oct 2002 08:17:46 -0400 (EDT) Received: from lubitsch.akademie.de (ns.akademie.de [62.165.4.3]) by postgresql.org (Postfix) with ESMTP id B7B9D475AA1 for ; Wed, 16 Oct 2002 08:17:45 -0400 (EDT) Received: from [62.165.4.160] (helo=there) by lubitsch.akademie.de with smtp (Exim 3.33 #2) id 181n7U-00050E-00; Wed, 16 Oct 2002 14:17:48 +0200 Content-Type: text/plain; charset="iso-8859-1" From: Ian Barwick Organization: akademie.de To: "Albrecht Berger" , "pgsql" Subject: Re: replace null with 0 in subselect ? Date: Wed, 16 Oct 2002 14:16:08 +0200 X-Mailer: KMail [version 1.3.2] References: <05f001c274fb$e8a5aba0$a07ba8c0@wxp> In-Reply-To: <05f001c274fb$e8a5aba0$a07ba8c0@wxp> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/173 X-Sequence-Number: 9890 On Wednesday 16 October 2002 12:07, Albrecht Berger wrote: > Hello, > I have a statement like this : > > INSERT INTO tab1 (c1, c2, c3) VALUES (1,2, SELECT MAX(pos)+1 FROM tab2); > > This works fine if the subselect returns a value, but if it returns > null there is a problem. In this case a 0 has to be inserted. > Is there any pg function which solves this problem ? > I know that oracle has a function but I didn't find > something similar in pg. A quick and dirty solution (untested): INSERT INTO tab1 (c1, c2, c3) VALUES (1,2, (SELECT CASE WHEN (SELECT MAX(pos)+1 ) IS NULL THEN 0 ELSE (SELECT MAX(pos)+1 ) END) ) Ian Barwick barwick@gmx.net