Re: How to return a default value if no result - Mailing list pgsql-novice

From Rob - TEAM Systems Ltd
Subject Re: How to return a default value if no result
Date
Msg-id 004001cfbe1e$4b757940$e2606bc0$@teamsystems.co.uk
Whole thread Raw
In response to Re: How to return a default value if no result  (David G Johnston <david.g.johnston@gmail.com>)
Responses Re: How to return a default value if no result  (David G Johnston <david.g.johnston@gmail.com>)
List 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@postgresql.org
[mailto:pgsql-novice-owner@postgresql.org] On Behalf Of David G Johnston
Sent: 22 August 2014 15:07
To: pgsql-novice@postgresql.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@postgresql.org) To make
changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice




pgsql-novice by date:

Previous
From: David G Johnston
Date:
Subject: Re: How to return a default value if no result
Next
From: David G Johnston
Date:
Subject: Re: How to return a default value if no result