How can I create a view with one column as CSV of several other columns?

advertisements

I have a table with columns bar1, bar2, bar3. Is there a way I can create a view with a column bar which is a CSV of bar1, bar2, and bar3?

Also, if bar2 is null or bar3 is null, then I don't want extraneous commas.

bar1 bar2 bar3   bar
---- ---- ----   ---
bat  bats bass = bat,bats,bass
bim  bis       = bim,bis


One way to do it with a case expression.

select trim(trailing ',' from
            case when bar1 is not null then bar1||',' end ||
            case when bar2 is not null then bar2||',' end ||
            case when bar3 is not null then bar3 end) bar
from t

Sample Demo