0

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 ??

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

3 Comments

That's what you say. I say that it works. If you provided test case (CREATE TABLE and INSERT INTO statements), I wouldn't have to to create my own. What you need is lines #6 onward.
dual is a built-in Oracle table that's frequently used for generating sample data. docs.oracle.com/cd/B19306_01/server.102/b14200/queries009.htm
@yarabyarab: it works perfectly
0

You can run something like this query:

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

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.