2

I'm trying to concatenate the values of a column.

For EX:

select case when month=1 then month_name1 
when month =12 then month_name2 END
from Calendar
where month in (1,12)

results:

Jan
Dec

Target results:

Jan - Dec

tried different ways but couldn't get it. looks very simple but hard luck. Thanks in advance.

2
  • Is using PL/SQL an option? Commented Feb 19, 2020 at 0:20
  • Not there yet :( Commented Feb 19, 2020 at 0:22

3 Answers 3

2

You can use aggregate function max as following:

select max(case when month=1 then month_name1 end)
       || ' - '||
       max(case when month=12 then month_name2 end)
from Calendar
where month in (1,12);

Cheers!!

Sign up to request clarification or add additional context in comments.

2 Comments

Don't you feel in your logic, you may need to add concatenation operator before 2nd iteration of max function.
No @SaiprasadBane. Test it and you will know the result.
1

I feel you would need a hack here, below query must do the magic !!

select c1.month_name1 || ' - ' || c2.month_name2 from Calendar c1, calendar c2 where c1.month = 1 and c2.month = 12 ;

Comments

0

Try this query using listagg

select listagg(case when month=1 then month_name1 
               when month =12 then month_name2 END, ' - ')
         within group (order by month)
from Calendar
where month in (1,12)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.