Concatenate multiple values and removing characaters when null

John

I have the below query

select id, CORD.LABEL ||' ('|| CORD.INTERVENT_TYPE || ')' ||' '||REFER.LABEL ||' ('|| REFER.INTERVENT_TYPE || ')'||' '|| INTER.LABEL ||' ('|| INTERVENT_TYPE || ')' AS INTERVEN_TYPE
from tableA

This is producing the below results.

ID    INTERVEN_TYPE
1      () () Education(Other)

I would like to display results only when the values are not null. I would expect to see.

ID    INTERVEN_TYPE
1     Education(Other)

I've tried case expression, nvl and coalesce. Which one would work best as I cannot create the correct syntax?

forpas

With CASE:

select 
id, 
TRIM(
CASE CORD.LABEL ||'('|| CORD.INTERVENT_TYPE || ')' 
    WHEN '()' THEN ''
    ELSE CORD.LABEL ||' ('|| CORD.INTERVENT_TYPE || ')' 
END 
||' '||
CASE REFER.LABEL ||'('|| REFER.INTERVENT_TYPE || ')'
    WHEN '()' THEN ''
    ELSE REFER.LABEL ||' ('|| REFER.INTERVENT_TYPE || ')'
END 
||' '|| 
CASE INTER.LABEL ||'('|| INTERVENT_TYPE || ')'
    WHEN '()' THEN ''
    ELSE INTER.LABEL ||' ('|| INTERVENT_TYPE || ')'
END 
)
AS INTERVEN_TYPE
from tableA

I removed now 1 space from the left parenthesis.
EDIT: Added TRIM function.
Maybe this would work too, if you eliminate () by REPLACE:

select 
id, TRIM(
REPLACE(CORD.LABEL ||' ('|| CORD.INTERVENT_TYPE || ')', '()')
||' '||
REPLACE(REFER.LABEL ||' ('|| REFER.INTERVENT_TYPE || ')', '()')
||' '|| 
REPLACE(INTER.LABEL ||' ('|| INTERVENT_TYPE || ')', '()')
) 
AS INTERVEN_TYPE
from tableA

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Concatenate multiple columns, with null values

null values in List<string> when adding and removing in multiple threads

How do you concatenate multiple columns in a DataFrame into a another column when some values are null?

Concatenate multiple node values when parsing XML

Concatenate multiple columns of dataframe with a seperating character for Non-null values

How to concatenate the multiple column,when anyone of the column is having null value

Concatenate column values when header is Matching from multiple files

Oracle concatenate String null values

Concatenate results with some null values

Removing Null Values from a Query

Restructure table by removing NULL values

Exclude Null Entries when concatenate

Object mapper removing empty and null values when converting object to string Java

Is there a default Pandas method for removing null or missing values when they are represented by a custom value like "?" or "Unknown"

Concatenate two columns of spark dataframe with null values

concatenate values in dataframe if a column has specific values and None or Null values

GROUP BY with SUM without removing empty (null) values

Pyspark Removing null values from a column in dataframe

Removing null values from Python csv import

How to concatenate multiple values in react state array

Concatenate values into multiple columns based on associated value

MS SQL Concatenate multiple values into a single column

Concatenate or Combine Multiple Values and Retain Fonts and Colour

Concatenate multiple values in same row into a list

Recursively iterate over multiple arrays to concatenate the values

Concatenate the values of multiple arrays on VBA for Excel

SSRS - Concatenate Multiple Parameter Values in a Text Box

Concatenate rows based on multiple column values

How to query and space address with multiple values when some are NULL