【问题标题】:Sanitize a string column with a mix of datetime and timestamps混合使用日期时间和时间戳来清理字符串列
【发布时间】:2019-10-28 14:29:55
【问题描述】:

我在 BigQuery 上有一个 String 数据类型的列,其中混合了日期和时间戳作为字符串。 https://www.evernote.com/l/AmOc0thoaMRLJ7y1IFnLMxwLAeREujUtGRc

尝试了 SAFE_CAST、DATE_PARSE 但都不起作用。

我希望能够将该列统一查询为时间戳。

【问题讨论】:

    标签: sql google-bigquery timestamp hubspot


    【解决方案1】:

    我希望能够将该列统一查询为时间戳。

    以下 BigQuery 标准 SQL 示例

    #standardSQL
    SELECT close_date, 
      COALESCE(
        TIMESTAMP_MILLIS(SAFE_CAST(close_date AS INT64)),
        SAFE.PARSE_TIMESTAMP('%m/%d/%y', close_date)
      ) AS close_date_as_timestamp
    FROM `project.dataset.table`   
    

    如果应用于您的示例数据 - 结果是

    Row close_date      close_date_as_timestamp  
    1   1556064000000   2019-04-24 00:00:00 UTC  
    2   01/24/19        2019-01-24 00:00:00 UTC  
    3   1548892800000   2019-01-31 00:00:00 UTC  
    4   11/27/18        2018-11-27 00:00:00 UTC    
    

    注意:您可以向 COALESCE 添加任意数量的数据模式

    例如你可以在下面添加支持2019-01-01

    SAFE.PARSE_TIMESTAMP('%Y-%m-%d', close_date)   
    

    等等……

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2019-03-31
      • 2012-10-25
      • 2023-03-27
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      相关资源
      最近更新 更多