Skip to content
Advertisement

Comma-delimit multiple columns when some values may be null

I need to comma-delimit the values contained in multiple columns, when each of the columns could contain a NULL value:

Results:

Is there an approach that is less cumbersome than what I’ve done for abc, preferably without having to use PL/SQL?

Advertisement

Answer

The function that you really want is concat_ws(), but Oracle doesn’t have one. But here is an alternative that is a bit less cumbersome than your method:

This creates the string with the commas. Oracle ignores the NULL values in a string concatenation. It then removes duplicate commas and leading and trailing commas.

Another method is:

I actually like this version better. It is more generalizable; the intermediate result doesn’t have a bunch of comma gumming up the middle of the string.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement