sql >> Base de Datos >  >> RDS >> Oracle

Oracle:Concat con delimitador, pero solo si ambos operandos NO SON NULOS

Sé que estás usando 10 g, así que eso no funcionará. Pero para completar, LISTAGG() maneja NULL valores "correctamente". Sin embargo, para eso tendrías que actualizar a 11g2:

-- Some sample data, roughly equivalent to yours
with t as (
  select 'foo' as x from dual union all
  select null       from dual union all
  select 'bar'      from dual
)
-- Use the listagg aggregate function to join all values
select listagg(x, ';') within group (order by rownum)
from t;

O un poco más breve, si desea enumerar las columnas de una tabla:

-- I use SYS.ORA_MINING_VARCHAR2_NT as a TABLE TYPE. Use your own, if you prefer
select listagg(column_value, ';') within group (order by rownum)
from table(ORA_MINING_VARCHAR2_NT('foo', null, 'bar'));

O contra una tabla real:

select listagg(column_value, ';') 
       within group (order by rownum)
from Table1
cross join table(ORA_MINING_VARCHAR2_NT(Table1.a, Table1.b, Table1.c))
group by Table1.id;

Ahora no estoy seguro si esto es mucho mejor (más legible) que tu ejemplo original :-)