2

How can change my SQL to return a default value if no rows are found?

Eg:

select text,name from text_file where text='met'

If no rows are found, I would like text to contain 'Values not found' for example

5
  • 1
    Please tag your question appropriately. Are you using MySQL or Oracle? Commented May 28, 2016 at 12:27
  • Possible duplicate of How to show 0 when no row found Commented May 28, 2016 at 13:10
  • 3
    this should be done in the presentation layer and not in SQL code because of many reason. last but not least how could you use this solution when all output columns are not text (eg date, int, guid)? Commented May 28, 2016 at 13:20
  • I do not agree. You could do it with SQL as it is powerful enough to handle this simple case. SQL could do computations and not just CRUD. Commented Oct 16, 2020 at 9:15
  • I guess that your condition is name='met' instead of text='met'. Commented Oct 16, 2020 at 9:16

4 Answers 4

3

One method uses union all:

select text, name
from text_file
where text = 'met'
union all
select max('Values not found'), NULL
from text_file
where text = 'met'
having count(*) = 0;

Notes. The second subquery is an aggregation query. It always returns one row (without the having). This row has what you are looking for.

Second, this type of operation should really be done in the application and not the database.

Third, if you are only expecting one row, then you can use an aggregation query such as:

select (case when count(*) > 0 then text else 'Values not found' end) as text,
       (case when count(*) > 0 then name end) as name
from text_file
where text = 'met'
Sign up to request clarification or add additional context in comments.

Comments

1

Full outer join used like cross join (1=1) gives the requested result.

Version for Oracle:

select nvl(tf.text, er.text) as text, 
       tf.name
from 
(select 'Values not found' text from dual ) er
 full outer join 
text_file tf
on 1 =1

There is no need to group by or execute the query multiple time like in other solutions.

1 Comment

IMHO the condition of the join should be tf.text = 'met' otherwise you don't use the OP condition at all.
0
IF Exists (select * from text_file where text='met')
      Begin
           Select text,name From text_file         Where text='met'
      End
Else
      Begin
            Select 'Values not found'
      End

Comments

0

Based on the answer by @dcieslak, here is my MWE in PostgreSQL.

create table text_file (name varchar(100), text varchar(100));

select
  coalesce(tf.name, default_value.name) as name,
  coalesce(tf.text, default_value.text) as text
from text_file tf right outer join
     (select 'Default name' as name, 'Values not found' as text) default_value
     on tf.name = 'met' ;

insert into text_file values ('met', 'met text'), ('foo', 'bar');
-- execute query here
/*
 name |   text
------+----------
 met  | met text
(1 row)
*/

delete from text_file where name = 'met';
-- execute query here
/*
     name     |       text
--------------+------------------
 Default name | Values not found
(1 row)
*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.