1

How in can I return an array in Oracle similar to PostgreSQL's ARRAY_AGG function?

SELECT
    ARRAY_AGG(EMAIL)
FROM
    EMPLOYEES
WHERE
    EMAIL IS NOT NULL
AND
    ORGANIZATION_ID IN (27623,27734)
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • 1
    Duplicate of [Equivalent of PostgreSQL's array\_agg in Oracle XE 11.2](https://stackoverflow.com/questions/47856440/equivalent-of-postgresqls-array-agg-in-oracle-xe-11-2) – Kaushik Nayak Apr 01 '19 at 07:01
  • Well, if I use `LISTAGG` function which is recommended in this post it raise error: `ORA-01489: result of string concatenation is too long`. Do you have any other ideas? – Nurzhan Nogerbek Apr 01 '19 at 08:33
  • Google the error : you will get several other alternatives – Kaushik Nayak Apr 01 '19 at 08:46
  • In Google I found recommendation to use `XMLAGG` function but I don't know it is best practice in that case. I use `RTRIM(XMLAGG(XMLELEMENT(e, EMAIL, ',').EXTRACT('//TEXT()')).GetClobVal(), ',') EMAILS` and it works. – Nurzhan Nogerbek Apr 01 '19 at 08:48
  • You have 2 options, either completely rethink the way you need to store and use such large data, i.e. avoiding concatenation altogether. If it isn't possible then go with the alternative ( like xmlagg, user defined functions using clob ,etc) – Kaushik Nayak Apr 01 '19 at 08:52

1 Answers1

1

use LISTAGG()

SELECT
    LISTAGG(EMAIL) WITHIN GROUP (ORDER BY EMAIL) as email
FROM
    EMPLOYEES
WHERE
    EMAIL IS NOT NULL
AND
    ORGANIZATION_ID IN (27623,27734)
Fahmi
  • 37,315
  • 5
  • 22
  • 31