X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5DF8C53746 for ; Mon, 9 May 2005 14:35:14 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 58257-06 for ; Mon, 9 May 2005 17:35:09 +0000 (GMT) Received: from zhonka1.zhonka.net (zhonka1.zhonka.net [66.228.195.5]) by svr1.postgresql.org (Postfix) with ESMTP id 8D9A153E69 for ; Mon, 9 May 2005 14:35:08 -0300 (ADT) Received: from wolf.pjkh.com ([66.228.196.74]) by zhonka1.zhonka.net (Post.Office MTA v3.5.3 release 223 ID# 0-58414U4500L450S0V35) with ESMTP id net; Mon, 9 May 2005 10:18:09 -0700 Received: from localhost (localhost [127.0.0.1]) by wolf.pjkh.com (Postfix) with ESMTP id 6418358AB; Mon, 9 May 2005 10:18:04 -0700 (PDT) Received: from wolf.pjkh.com ([127.0.0.1]) by localhost (wolf.pjkh.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 26108-02; Mon, 9 May 2005 10:18:04 -0700 (PDT) Received: by wolf.pjkh.com (Postfix, from userid 1000) id 303CF58AA; Mon, 9 May 2005 10:18:04 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by wolf.pjkh.com (Postfix) with ESMTP id 2985058A9; Mon, 9 May 2005 10:18:04 -0700 (PDT) Date: Mon, 9 May 2005 10:18:04 -0700 (PDT) From: Philip Hallstrom To: Mark Fenbers Cc: pgsql-sql@postgresql.org Subject: Re: default value for select? In-Reply-To: <427F9685.8050104@noaa.gov> Message-ID: <20050509101745.F26087@wolf.pjkh.com> References: <427F9685.8050104@noaa.gov> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by amavisd-new at pjkh.com X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200505/91 X-Sequence-Number: 21404 > I want to update a column in myTable. The value this column is set to depends on a > nested select statement which sometimes returns 0 rows instead of 1. This is a > problem since the column I'm trying to update is set to refuse nulls. Here's a > sample: > > update myTable set myColumn = (Select altColumn from altTable where altColumn != > 'XXX' limit 1) where myColumn = 'XXX'; > > MyColumn cannot accept nulls, but sometimes "Select altColumn ..." returns 0 rows, > and thus, the query fails. > > Is there a way to set a default value to be inserted into myColumn if and when > "select altColumn ..." returns zero rows? COALESCE(value [, ...]) The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null. This is often useful to substitute a default value for null values when data is retrieved for display, for example: SELECT COALESCE(description, short_description, '(none)') ... Like a CASE expression, COALESCE will not evaluate arguments that are not needed to determine the result; that is, arguments to the right of the first non-null argument are not evaluated.