From Mark.Fenbers@noaa.gov Mon May 9 16:58:04 2005 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 B62F153E53 for ; Mon, 9 May 2005 13:58:04 -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 51162-10 for ; Mon, 9 May 2005 16:57:58 +0000 (GMT) Received: from werhmsg.werh.noaa.gov (werhmsg.werh.noaa.gov [198.206.32.236]) by svr1.postgresql.org (Postfix) with ESMTP id 7665D534E2 for ; Mon, 9 May 2005 13:57:57 -0300 (ADT) Received: from [198.206.42.50] ([198.206.42.50]) by werhmsg.werh.noaa.gov (Netscape Messaging Server 4.15) with ESMTP id IG8F4L00.11O for ; Mon, 9 May 2005 12:57:57 -0400 Message-ID: <427F9685.8050104@noaa.gov> Date: Mon, 09 May 2005 12:57:41 -0400 From: "Mark Fenbers" Organization: DoC/NOAA/NWS/OHRFC User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: default value for select? Content-Type: multipart/mixed; boundary="------------090904020309000109020002" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.089 tagged_above=0 required=5 tests=AWL, HTML_30_40, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200505/88 X-Sequence-Number: 21401 This is a multi-part message in MIME format. --------------090904020309000109020002 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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?

Mark
--------------090904020309000109020002 Content-Type: text/x-vcard; charset=utf-8; name="Mark.Fenbers.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Mark.Fenbers.vcf" begin:vcard fn:Mark Fenbers n:Fenbers;Mark org:DoC/NOAA/NWS/OHRFC adr:;;1901 South SR 134;Wilmington;OH;45177-9708;USA email;internet:Mark.Fenbers@noaa.gov title:Sr. HAS Meteorologist tel;work:937-383-0430 x246 x-mozilla-html:TRUE url:http://weather.gov/ohrfc version:2.1 end:vcard --------------090904020309000109020002-- From postgresql@philip.pjkh.com Mon May 9 17:35:14 2005 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. From keithw@narrowpathinc.com Mon May 9 17:27:31 2005 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 7BDFA53E68 for ; Mon, 9 May 2005 14:27:31 -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 58084-01 for ; Mon, 9 May 2005 17:27:26 +0000 (GMT) Received: from mail.99main.com (mail.99main.com [208.28.184.11]) by svr1.postgresql.org (Postfix) with ESMTP id A4F5053E5E for ; Mon, 9 May 2005 14:27:25 -0300 (ADT) Received: from 99main.com (localhost [127.0.0.1]) by mail.99main.com (8.12.8+Sun/8.12.8) with ESMTP id j49HQtLS009203; Mon, 9 May 2005 13:26:56 -0400 (EDT) From: "Keith Worthington" To: "Mark Fenbers" Cc: pgsql-sql@postgresql.org Reply-To: KeithW@narrowpathinc.com Subject: Re: default value for select? Date: Mon, 9 May 2005 13:26:55 -0400 Message-Id: <20050509172414.M48914@narrowpathinc.com> In-Reply-To: <427F9685.8050104@noaa.gov> References: <427F9685.8050104@noaa.gov> X-Mailer: Open WebMail 2.50 20050106 X-OriginatingIP: 68.235.22.242 (npkeithw) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.01 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/89 X-Sequence-Number: 21402 On Mon, 09 May 2005 12:57:41 -0400, Mark Fenbers wrote > I want to update a column in myTable. The value this column is set > todepends on a nested select statement which sometimes returns 0 > rowsinstead of 1. This is a problem since the column I'm trying to > updateis set to refuse nulls. Here's a sample: > > update myTable set myColumn = (Select altColumn from altTable > wherealtColumn != '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 > ifand when "select altColumn ..." returns zero rows? > > Mark Mark, I do not know if it will work but I would try the COALESCE function. http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html Kind Regards, Keith From ajwasson@gmail.com Mon May 9 17:30:58 2005 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 EF94653E67 for ; Mon, 9 May 2005 14:30:58 -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 58665-01 for ; Mon, 9 May 2005 17:30:54 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.195]) by svr1.postgresql.org (Postfix) with ESMTP id B023453DC4 for ; Mon, 9 May 2005 14:30:53 -0300 (ADT) Received: by wproxy.gmail.com with SMTP id 68so2141316wra for ; Mon, 09 May 2005 10:30:54 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=HY2FH8W5bFTbd7O92gywA0pwhYM7iQ2O+wj6JJjOUDEEFyKaxLBrU1BVqTUidl1unx4HV/BJtsaNq0eOD5KQrmUYmvqNR//GttC5X/46W3j46aqRNEpfAt97hjirxsGP0fyRmnObSGeyr5dt8M4dfd5aGDNorNLsCWZxUTnk2Co= Received: by 10.54.34.28 with SMTP id h28mr2675281wrh; Mon, 09 May 2005 10:30:53 -0700 (PDT) Received: by 10.54.65.12 with HTTP; Mon, 9 May 2005 10:30:53 -0700 (PDT) Message-ID: <6d8daee30505091030f5c7828@mail.gmail.com> Date: Mon, 9 May 2005 10:30:53 -0700 From: Tony Wasson Reply-To: Tony Wasson To: Mark Fenbers Subject: Re: default value for select? Cc: pgsql-sql@postgresql.org In-Reply-To: <427F9685.8050104@noaa.gov> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <427F9685.8050104@noaa.gov> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.273 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200505/90 X-Sequence-Number: 21403 On 5/9/05, Mark Fenbers wrote: > 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 inste= ad > of 1. This is a problem since the column I'm trying to update is set to > refuse nulls. Here's a sample: > =20 > update myTable set myColumn =3D (Select altColumn from altTable where > altColumn !=3D 'XXX' limit 1) where myColumn =3D 'XXX'; > =20 > MyColumn cannot accept nulls, but sometimes "Select altColumn ..." retur= ns > 0 rows, and thus, the query fails. =20 > =20 > Is there a way to set a default value to be inserted into myColumn if an= d > when "select altColumn ..." returns zero rows? > =20 > Mark Mark,=20 You can work around this by using a CASE statement. In this case, test for a NULL from your subquery. This is not elegant at all, but it should do what you are wanting. update myTable set myColumn =3D (CASE WHEN (Select altColumn from altTable where altColumn !=3D 'XXX' limit 1) IS NULL THEN 'some default value' ELSE (Select altColumn from altTable where altColumn !=3D 'XXX' limit 1) END) where myColumn =3D 'XXX'; Hope this helps... Tony