[英]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_TIMESTAMP
和TIMESTAMP
函数。 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.