如何用纳秒解析时间戳

how to parse timestamp with nanoseconds

我们的数据中有一个类似于 2021-05-03T14:25:30.195737792Z 的时间戳,Big Query (```CAST('2021-54-03T14:25:30.195737792Z' AS timestamp)```) 失败并出现以下错误:

Invalid timestamp: '2021-05-03T14:25:30.195737792Z'

如何将带有纳秒的时间戳转换为例如UTC 格式的微秒?

试试下面

select ts_as_string, 
  timestamp(regexp_replace(ts_as_string, r'([^.]+)(.)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?', r'')) ts_as_timestamp
from your_table              

如果像您的示例一样应用于虚拟数据 - 输出为

较短的版本是

timestamp(regexp_replace(ts_as_string, r'([^.]+)(.)?(\d{0,6})(\d)*', r''))