5

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 Answers 2

7
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

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.