Snowflake SQL 上带有空值的字符串连接

Fel*_*ffa 3 sql snowflake-cloud-data-platform

我有 3 列(名字、中间名、姓氏),我想连接 3 个字符串(以构造全名)。

但是,如果其中任何一个值为 null,则结果也为 null。

当其中一些字符串可能为空时,连接字符串的优雅且安全的方法是什么?

Fel*_*ffa 5

NVL()/IFNULL()和的组合CONCAT_WS()可以使用,但我更喜欢以下内容:

select array_to_string(array_construct_compact(column1, column2, column3), ' ')
from values('a', 'b', 'c'), ('a', null, 'c')
Run Code Online (Sandbox Code Playgroud)
  • array_construct_compact()删除空值。
  • array_to_string()连接时添加必要的空格。

https://docs.snowflake.com/en/sql-reference/functions/array_construct_compact.html