presto等价于concat\u ws

isr3a4wc  于 2021-07-26  发布在  Java
关注(0)|答案(4)|浏览(1640)

我在找一个函数,在presto中用下划线这样的分隔符连接两列。

nwo49xxi

nwo49xxi1#

要处理:

select concat_ws(',', col1, col2)

您可以使用:

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

                     ),
                2
             )

这会将非空值连接到一个字符串中。结果字符串将以逗号开头。这个 substr() 删除第一个字符。

4dc9hkyq

4dc9hkyq2#

你要找的是 array_join 功能,见文件。
数组\u连接(x,分隔符,空\u替换)→ 瓦尔查尔
使用delimiter和可选字符串连接给定数组的元素以替换空值。
例子:
列是c1,c2您可以添加更多当然:

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

结果将是:
1_2
3_4
5不适用
7_8

qncylg1j

qncylg1j3#

它已经被添加到prestosql(现在的trino)中,并在几个版本中恢复:https://trino.io/docs/current/functions/string.html

concat_ws(string0, string1, ..., stringN) → varchar#
Returns the concatenation of string1, string2, ..., stringN using string0 as a separator. If string0 is null, then the return value is null. Any null values provided in the arguments after the separator are skipped.

concat_ws(string0, array(varchar)) → varchar
Returns the concatenation of elements in the array using string0 as a separator. If string0 is null, then the return value is null. Any null values in the array are skipped.
jobtbby3

jobtbby34#

select concat(col1, ',', col2)

相关问题