Re: default value for select? - Mailing list pgsql-sql

From Philip Hallstrom
Subject Re: default value for select?
Date
Msg-id 20050509101745.F26087@wolf.pjkh.com
Whole thread Raw
In response to default value for select?  ("Mark Fenbers" <Mark.Fenbers@noaa.gov>)
List pgsql-sql
> 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.


pgsql-sql by date:

Previous
From: Tony Wasson
Date:
Subject: Re: default value for select?
Next
From: joffer
Date:
Subject: unsuscribe