sql - Snowflake SQL 中字符串与 null 的连接

标签 sql snowflake-cloud-data-platform

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

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

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

最佳答案

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

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

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

关于sql - Snowflake SQL 中字符串与 null 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66754565/

相关文章:

database - 共享数据库上的数据屏蔽策略错误

snowflake-cloud-data-platform - 如何处理大数据文件格式和外部表中区分大小写的列名?

SQL 使用来自 CASE EXISTS 的值

sql - 如何在查询公式中有多个标签?

sql - 按字符降序显示数据 - postgresql

mysql - 如何消除查询中的某些值

python - 雪花连接器 SQL 编译错误 : maximum number of expressions in a list exceeded, 预计最多 16,384

mysql - 选择查询返回错误结果

php sql 查询和 php 变量的总和

sql - Snowflake 中的 STDDEV Windows 函数 : not getting a uniform value according to the partition