yarab yarab
yarab yarab

Reputation: 27

Concat a string based on condition in oracle sql developer

I want to concat to the output based on condition. Here is my query:

select 'hey',
  case id =1 then 'Mary' else 'Tom' end 
from names;'

I want to print 'hey tom' or 'hey mary' based on id ... any help ??

Upvotes: 0

Views: 1774

Answers (2)

Tim F
Tim F

Reputation: 1

You can run something like this query:

SELECT CONCAT('hey ', FIRST_NAME) from names where ID = '1';

Upvotes: 0

Littlefoot
Littlefoot

Reputation: 142713

It is concatenation you need; in Oracle, double pipe || represents that operator:

SQL> with names (id) as
  2    (select 1 from dual union all
  3     select 2 from dual union all
  4     select 3 from dual
  5    )
  6  select id,
  7    'hey ' || case when id = 1 then 'Mary'
  8                   else 'Tom'
  9              end result
 10  from names;

        ID RESULT
---------- ----------
         1 hey Mary
         2 hey Tom
         3 hey Tom

SQL>

Upvotes: 3

Related Questions