请参阅下面的两个选项。两者都适用于 BigQuery 标准 SQL
#standardSQL
SELECT
REPLACE(REGEXP_REPLACE(TO_JSON_STRING(natural_person_date_of_birth_list), r'\[|\]|"', ''), ',', '|') dates_string_1,
(SELECT STRING_AGG(FORMAT_DATE('%Y-%m-%d', d), "|")
FROM UNNEST(natural_person_date_of_birth_list) d) AS dates_string_2
FROM `project.dataset.table`
以下内容仅供您快速测试和使用:
#standardSQL
WITH `project.dataset.table` AS (
SELECT [CURRENT_DATE(), DATE '2011-10-15'] natural_person_date_of_birth_list
)
SELECT
REPLACE(REGEXP_REPLACE(TO_JSON_STRING(natural_person_date_of_birth_list), r'\[|\]|"', ''), ',', '|') dates_string_1,
(SELECT STRING_AGG(FORMAT_DATE('%Y-%m-%d', d), "|")
FROM UNNEST(natural_person_date_of_birth_list) d) AS dates_string_2
FROM `project.dataset.table`
两个选项都返回我认为预期的结果:
dates_string_1 dates_string_2
2017-11-01|2011-10-15 2017-11-01|2011-10-15
注意:第二个选项的好处是您可以控制列表中日期的顺序,例如
#standardSQL
WITH `project.dataset.table` AS (
SELECT [CURRENT_DATE(), DATE '2011-10-15'] natural_person_date_of_birth_list
)
SELECT
REPLACE(REGEXP_REPLACE(TO_JSON_STRING(natural_person_date_of_birth_list), r'\[|\]|"', ''), ',', '|') dates_string_1,
(SELECT STRING_AGG(FORMAT_DATE('%Y-%m-%d', d), "|" ORDER BY d)
FROM UNNEST(natural_person_date_of_birth_list) d
) AS dates_string_2
FROM `project.dataset.table`
这里的输出将是(注意 dates_string_2 的变化)
dates_string_1 dates_string_2
2017-11-01|2011-10-15 2011-10-15|2017-11-01