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
Some databases support a function called concat_ws()
that does exactly this.
In Oracle, it is easy enough for three columns:
select trim(both ',' from replace(bar1 || ',' || bar2 || ',' || bar3, ',,', ',')) as bar
from . . .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.