Comma-Delimited List with "and" concatenated before Last Element in Oracle SQL -
I am using LISTAGG to create a comma-delimited list for a report, but I have "and" the last element First of all, there is just my output for 3 different cases:
First, second, third first, second first
but now the desired output is :
I had thought of doing a case when he examines the count and if If 2 is, then 'and' as the delimiter, but how can I go about doing this for more than 2 elements?
Thanks
You can try regexp_replace:
< Select pre-> duplex to regexp_replace ('first, second, third', ', ([^,] +) $', 'and' 1 ');
, ([^,] +)
until the end of the last comma + string and anything else searches for a comma and changes it with "and" First expression
(For example, if your comma does not include a comma then this should work)
Full example:
select regexp_replace ( Select the name 'first' from the double association between listagg (name, ',') group (sequence by name), ', ([^,] +) $', 'and' 1 ') 'Third' from dual);
Comments
Post a Comment