2
votes

I have two string type fields "Source" and "Detail" with either of this having null value. Now I'm concatenating both the string fields separated by hyphen(-); but due to NULL value, the output is also NULL.

Refer image consisting of the actualoutput and outputexpected (highlighted yellow).

Can someone please suggest how to get the output expected?

enter image description here

Query I used:

SELECT Source, Detail, CONCAT(cast(Source as string), "-", cast(Detail as string)) AS actualoutput FROM tablename

2

2 Answers

5
votes
SELECT Source, Detail, CONCAT(IFNULL(Source, ''), "-", IFNULL(Detail, '')) AS actualoutput 
FROM tablename   

Note: as both "Source" and "Detail" are already of type STRING - you can omit CAST'ing to STRING

3
votes

Use COALESCE to replace NULL with empty string:

SELECT
    Source,
    Detail,
    CONCAT(COALESCE(Source, ''), '-', COALESCE(Detail, '')) AS actualoutput
FROM tablename;

Note that anything concatenated with NULL will always yield NULL.