我有 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/