Fel*_*ffa 5 sql google-bigquery
当任何值为 NULL 时,CONCAT() 返回 NULL。我必须使用 IFNULL() 来包装传递给 CONCAT() 的所有字段。是否有忽略 NULL 的 CONCAT() 变体?
例如:
#standardSQL
WITH data AS (
SELECT 'a' a, 'b' b, CAST(null AS STRING) nu
)
SELECT CONCAT(a, b, nu) concatenated, ARRAY_TO_STRING([a,b,nu], ',') w_array_to_string
FROM `data`
--->
null
Run Code Online (Sandbox Code Playgroud)
Mik*_*ant 10
快的 Jam Session讨论有趣的主题
现实生活中的用例可能无限组合 以下是一些变化:
#standardSQL
WITH data AS (
SELECT 'a' a, 'b' b, 'c' c UNION ALL
SELECT 'y', 'x', NULL UNION ALL
SELECT 'v', NULL, 'w'
)
SELECT
*,
CONCAT(a, b, c) by_concat,
ARRAY_TO_STRING([a,b,c], '') by_array_to_string,
ARRAY_TO_STRING([a,b,c], '', '?') with_null_placeholder,
ARRAY_TO_STRING(
(SELECT ARRAY_AGG(col ORDER BY col DESC)
FROM UNNEST([a,b,c]) AS col
WHERE NOT col IS NULL)
, '') with_order
FROM `data`
Run Code Online (Sandbox Code Playgroud)
输出是:
a b c by_concat by_array_to_string with_null_placeholder with_order
- ---- ---- --------- ------------------ --------------------- ----------
y x null null yx yx? yx
a b c abc abc abc cba
v null w null vw v?w wv
Run Code Online (Sandbox Code Playgroud)
使用 ARRAY_TO_STRING([col1, col2, ...]) 代替:
#standardSQL
WITH data AS (
SELECT 'a' a, 'b' b, CAST(null AS STRING) nu
)
SELECT ARRAY_TO_STRING([a,b,nu], '') w_array_to_string
FROM `data`
--->
ab
Run Code Online (Sandbox Code Playgroud)