Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PSQL: How can I return a value from a select query, but if the resultset is null return a default vaule?

  DECLARE _retention bigint := (SELECT retention_days
          FROM   retention_period
          WHERE  fulfilment_type = 'daily');

If the SELECT query returns null because "daily" doesn’t exist in the retention_period table, how can I return a default value please?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use Postgres coalesce to set a default value.

DECLARE _retention bigint := coalesce((SELECT retention_days as retention_days
          FROM   retention_period
          WHERE  fulfilment_type = 'daily'), 1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading