Re: How to return a default value if no result

From: "Rob - TEAM Systems Ltd" <rob(at)teamsystems(dot)co(dot)uk>
To: <pgsql-novice(at)postgresql(dot)org>
Subject: Re: How to return a default value if no result
Date: 2014-08-22 15:32:34
Message-ID: 004001cfbe1e$4b757940$e2606bc0$@teamsystems.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Thanks David,
That's actually what we did in the end, works fine in the case where there
is only one field (sorry, column!) in the result.
Just out of curiosity (luckily all the places I need to do this at the
moment will work with coalesce), is there a way to return a default result
with a multiple-column query, without resorting to unions or the long-winded
case statement?
Something like this would be nice (but I don't think such a syntax exists,
at least not that I can find):
SELECT col1, col2, col3 FROM mytable
WHERE key='A'
DEFAULT (0,'No','')

-----Original Message-----
From: pgsql-novice-owner(at)postgresql(dot)org
[mailto:pgsql-novice-owner(at)postgresql(dot)org] On Behalf Of David G Johnston
Sent: 22 August 2014 15:07
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: [NOVICE] How to return a default value if no result

Rob Northcott wrote
> Is there a nice way of forcing a default result somehow?

The Coalesce function is your friend:

SELECT COALESCE( (SELECT subquery), <default> )

David J.

--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-return-a-default-value-if-no-
result-tp5815850p5815860.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.

--
Sent via pgsql-novice mailing list (pgsql-novice(at)postgresql(dot)org) To make
changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message David G Johnston 2014-08-22 15:51:31 Re: How to return a default value if no result
Previous Message David G Johnston 2014-08-22 14:07:25 Re: How to return a default value if no result