Handy Regexp trick for Listaggs

September 27, 2016 at 7:46 pm | Posted in Oracle Developement | Leave a comment

I use the listagg function to generate a comma separated list of employees who work in department 10 from the EMP table.

To make the list more readable, I want to substitute the last comma with the word and.

 with q as (
 select listagg(ename,', ') within group (order by 1) ret
from emp
 where deptno = 10
 )
 select regexp_replace(ret,',',' and',instr(ret,',',-1)) ret
 from q;
SQL> 

RET
--------------------------------------------------------------------------------
CLARK, KING and MILLER

 

 

Advertisements

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Entries and comments feeds.

%d bloggers like this: