简体   繁体   中英

How can combine the values of multiple columns to a single comma separated column in Oracle SQL?

I need to create a column that has all the values from the previous columns combined and separated using commas ', '

I can't use listagg since I'm trying to combine multiple columns instead of rows.

below is an example of how the result column should look like, thanks.

例子

Use string concatenation:

select trim(leading ',' from
             (case when column1 is not null then ',' || column1 end) ||
             (case when column2 is not null then ',' || column2 end) ||
             (case when column3 is not null then ',' || column3 end)
           )

This is also using a string concatenation but slightly different

SELECT 
SUBSTR(
    REPLACE(
    ', ' || NVL(column1,'!!') || ', ' || NVL(column2,'!!') || ', ' || NVL(column3,'!!')
    ,', !!','')
,3,100)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM