简体   繁体   English

BigQuery - 使用默认值将字符串转换为时间戳

[英]BigQuery - convert String to Timestamp with default value

In the BigQuery I have String column which contains timestamp data.在 BigQuery 中,我有包含时间戳数据的String列。 In the query I want to convert it to the Timestamp column.在查询中,我想将其转换为Timestamp列。
I know, that BigQuery have PARSE_TIMESTAMP and TIMESTAMP functions.我知道,BigQuery 具有PARSE_TIMESTAMPTIMESTAMP函数。 Issue is, that this data comes from the external source, so I need to be ready for the possibility, that input String may not be correct timestamp.问题是,此数据来自外部源,因此我需要为输入String可能不是正确时间戳的可能性做好准备。
While using these functions inside the query it will fail, if input data is in the wrong format.在查询中使用这些函数时,如果输入数据格式错误,查询将失败。
Is there a method to let this query pass even with wrong input data?有没有一种方法可以让这个查询即使输入数据错误也能通过? For example, to change not correct format to null or some arbitrary timestamp like 1900-01-01 00:00:00 .例如,将不正确的格式更改为null或一些任意时间戳,如1900-01-01 00:00:00
Simple regexp check will not be sufficient.简单的正则表达式检查是不够的。 For example 2020-02-29 00:00:00 is a completely fine timestamp, but 2019-02-29 00:00:00 is incorrect.例如2020-02-29 00:00:00是一个完全正确的时间戳,但2019-02-29 00:00:00是不正确的。

You need to prefix with SAFE in order to get NULLs for invalid data.您需要使用 SAFE 作为前缀,以便为无效数据获取 NULL。

SELECT safe.timestamp('2019-02-29 00:00:00') as tt

returns回报

[
  {
    "tt": null
  }
]

If you begin a function with the SAFE.如果您使用 SAFE 开始一项功能。 prefix, it will return NULL instead of an error.前缀,它将返回 NULL 而不是错误。 The SAFE.外管局。 prefix only prevents errors from the prefixed function itself: it does not prevent errors that occur while evaluating argument expressions. prefix 仅防止前缀函数本身的错误:它不会防止在评估参数表达式时发生的错误。 The SAFE.外管局。 prefix only prevents errors that occur because of the value of the function inputs, such as "value out of range" errors; prefix 仅防止由于函数输入的值而发生的错误,例如“value out of range”错误; other errors, such as internal or system errors, may still occur.其他错误,例如内部或系统错误,可能仍会发生。 If the function does not return an error, SAFE.如果函数没有返回错误,SAFE。 has no effect on the output.对输出没有影响。 If the function never returns an error, like RAND, then SAFE.如果函数从不返回错误,如 RAND,则为 SAFE。 has no effect.没有效果。

https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#safe_prefix https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#safe_prefix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 pyarrow 时间戳(以 ms 为单位)转换为 BigQuery 时间戳 - Convert pyarrow timestamp in ms to BigQuery timestamp BigQuery中从字符串到时间戳的时间戳 - timstamp from string to timestamp in BigQuery 在 BigQuery 中加载 avro 文件 - 默认值的意外类型。 预期为 null,但找到了字符串:“null” - Load a avro file in BigQuery - Unexpected type for default value. Expected null, but found string: "null" 如何从 BigQuery 中的字符串中提取值 - How to extract the value from the string in BigQuery bigquery 与日期的时间戳差异 - timestamp difference in bigquery with date 如何在bigquery中使用正则表达式获取值字符串 - How to get value string with regexp in bigquery 如何从字符串中删除“R$”以将其转换为在 BigQuery 中浮动 - How to remove "R$ " from a string to convert it to float in BigQuery 我可以将包含字符串化数组的行值转换为 BigQuery 中的数组吗 - Can I convert row value contains of stringified array to array in BigQuery Vue Firebase - 将 Firestore 时间戳值数组转换为可读的 javascript 日期 - Vue Firebase - convert array of firestore timestamp value to readable javascript date 使用 BigQuery 将 CURRENT_TIMESTAMP 转换为 dd/mm/yyyy hh:mm 字符串 - Cast CURRENT_TIMESTAMP to dd/mm/yyyy hh:mm string with BigQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM