0

I want to convert an array<string> to string in hive. The array data is as follow:

+-------------------------------------+--+
| NULL                                |
| ["Extension","Terms & Conditions"]  |
| ["Value (generic or item level)"]   |
+-------------------------------------+--+

I want to collect array values to convert to string without [""] so that I could get result like:

+-------------------------------------+--+
| NULL                                |
| Extension,Terms & Conditions        |
| Value (generic or item level)       |
+-------------------------------------+--+

Following query: select concat_ws(',', col_name) as col_name from table_stg; provides the result but is is returning NULL as empty. I tried several reference like:

How can I convert array to string in hive sql?

Hive - How to cast array to string?

But not getting the desired result. Is there any way to get the desired result?

2
  • 1
    use a case expression. case when size(col_name) = 0 then null else concat_ws(',',col_name) end Commented Apr 17, 2020 at 15:22
  • Hi @VamsiPrabhala - checking size doesn't worked, but it gave me the idea to solve this. thanks for your comment :) Commented Apr 20, 2020 at 7:36

1 Answer 1

1

With reference to Vamsi comment, I managed to get this and thought to answer as well for the community reference.

select case when col_name is NULL then NULL 
    else concat_ws(',',col_name) end from table_name;
Sign up to request clarification or add additional context in comments.

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.