Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove nulls from an array in SQL

Want to remove nulls from an array in hive/sql

for example : array is ['1',null] after converting to string values it should be '1' only.

to split the array I am using below:

concat_ws( ",", array_val)

this gives : 1,null

required output : 1

Thanks for the help!

like image 859
MysticRenge Avatar asked Jun 25 '18 07:06

MysticRenge


People also ask

How do you get rid of NULLs in SQL?

There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.

What is Array_agg in SQL?

PostgreSQL ARRAY_AGG() function is an aggregate function that accepts a set of values and returns an array where each value in the input set is assigned to an element of the array. Syntax: ARRAY_AGG(expression [ORDER BY [sort_expression {ASC | DESC}], [...])

Which function ignore NULL values in SQL?

Answer: A. Except COUNT function, all the group functions ignore NULL values.


1 Answers

Use regexp_replace to remove null from concatenated string:

hive> select regexp_replace('null,1,2,null,2,3,null','(,+null)|(^null,)','');
OK
1,2,2,3
Time taken: 6.006 seconds, Fetched: 1 row(s)
like image 128
leftjoin Avatar answered Sep 21 '22 10:09

leftjoin