Bigquery iso8601 字符串到时间戳

Bigquery iso8601 string to timestamp

在我的数据集中,我有一个变量 duration,其中有 200 万行 ISO 8601 格式的数据。示例格式:PT21SPT5M29SPT1M16S

我在将此字符串变量转换为时间时遇到问题。目前我正在使用查询:

REGEXP_EXTRACT(duration, r'[^\d]+') as time,

通过这个查询可以区分时长是小时分钟还是秒。我想将字符串持续时间转换为时间。我使用了 Regex 和 cast 但没有成功。 当我尝试将字符串转换为时间时出现错误:

"Invalid timestamp: '15S'"

"Invalid timestamp: '1M'

"Bad int64 value: PT11M16S"

我 运行 遇到的最大问题是字符串永远不一样。

更多字符串示例:

PT48S
PT43M40S 
PT7M54S 
PT3M14S 
PT4H17M16S 
PT3M46S 
PT3H18M35S 
PT3H8M45S

更新: 我试过了

WITH data AS (
  SELECT (duration) as time_str FROM x
)
SELECT 
  CASE
    WHEN time_str LIKE 'PT%H%M%S' THEN PARSE_TIME('PT%HH%MM%SS', time_str)
    WHEN time_str LIKE 'PT%M%S' THEN PARSE_TIME('PT%MM%SS', time_str)
    WHEN time_str LIKE 'PT%S' THEN PARSE_TIME('PT%SS', time_str)
    ELSE NULL
  END
FROM data

我收到错误:

Mismatch between format character 'S' and string character 'H'

也许这样就可以了:

WITH data AS (
  SELECT * FROM UNNEST(['PT48S', 'PT43M40S', 'PT7M54S', 'PT3M14S', 'PT4H17M16S', 'PT3M46S', 'PT3H18M35S', 'PT3H8M45S']) as time_str
)
SELECT 
  CASE
    WHEN time_str LIKE 'PT%H%M%S' THEN PARSE_TIME('PT%HH%MM%SS', time_str)
    WHEN time_str LIKE 'PT%M%S' THEN PARSE_TIME('PT%MM%SS', time_str)
    WHEN time_str LIKE 'PT%S' THEN PARSE_TIME('PT%SS', time_str)
    ELSE NULL
  END
FROM data

考虑以下

select time_str,
  ( select parse_time('%H:%M:%S', string_agg(ifnull(lpad(val, 2, '0'), '00'), ':' order by offset))
    from unnest([
      regexp_extract(time_str, r'(\d+)H'), 
      regexp_extract(time_str, r'(\d+)M'), 
      regexp_extract(time_str, r'(\d+)S')
    ]) val with offset) time_time
from data           

如果应用于您问题中的示例数据 - 输出为