Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presto equivalent of CONCAT_WS

I'm looking for a function in Presto to concat two columns with a separator like underline.

like image 351
MTT Avatar asked Mar 02 '23 06:03

MTT


2 Answers

Your are looking here for the array_join function, see docs.

array_join(x, delimiter, null_replacement) → varchar

Concatenates the elements of the given array using the delimiter and an optional string to replace nulls.

Example:
columns are c1,c2 you can add more of course:

WITH  demo_table (c1,c2) AS 
    (SELECT * FROM  (VALUES  (1,2),(3,4),(5,null),(7,8) ))
SELECT array_join(array[c1,c2], '_', 'NA')
FROM demo_table

Results will be:
1_2
3_4
5_NA
7_8

like image 73
Chananel P Avatar answered Mar 13 '23 01:03

Chananel P


To handle:

select concat_ws(',', col1, col2)

You can use:

select substr( concat(case when col1 is not null then ',' || col1 else '' end,
                      case when col2 is not null then ',' || col2 else '' end

                     ),
                2
             )

This concatenates the non-NULL values into a string. The resulting string will start with a comma. The substr() removes the first character.

like image 20
Gordon Linoff Avatar answered Mar 12 '23 23:03

Gordon Linoff