简体   繁体   English

如何在 BigQuery SQL 中捕获失败的 CAST 语句?

[英]How to catch a failed CAST statement in BigQuery SQL?

We are converting a STRING field to a DATETIME field using BigQuery's non-legacy SQL.我们正在使用 BigQuery 的非旧版 SQL 将 STRING 字段转换为 DATETIME 字段。

The DATETIME fields are corrupted with values like "None" and "0.0" which causes our CAST statement to fail. DATETIME 字段被“None”和“0.0”等值损坏,这导致我们的 CAST 语句失败。

We see that for other types of SQL there are TRY-CATCH functions and ISNUMERIC() tests - neither of which appear to be supported in BigQuery.我们看到对于其他类型的 SQL 有 TRY-CATCH 函数和 ISNUMERIC() 测试——BigQuery 似乎不支持这两者。

Here's an example that catches "None" but fails to catch random floats or integers:这是一个捕获“无”但未能捕获随机浮点数或整数的示例:

CASE 
 WHEN UPDT_DT_TM LIKE 'None' THEN NULL
 ELSE CAST(UPDT_DT_TM AS DATETIME) 
END AS UPDT_DT_TM,

Apart from User-Defined-Functions (UDF) in BigQuery - is there any other way in create a CASE statement that can convert to DATETIME when it can but otherwise just leaves the value as NULL?除了 BigQuery 中的用户定义函数 (UDF) 之外,是否还有其他方法可以创建一个 CASE 语句,该语句可以转换为 DATETIME,但否则只会将值保留为 NULL?

You can use the SAFE_CAST function, which returns NULL if the input is not a valid value when interpreted as the desired type. 您可以使用SAFE_CAST函数,如果输入在解释为所需类型时不是有效值,则该函数返回NULL。 In your case, you would just use SAFE_CAST(UPDT_DT_TM AS DATETIME) . 就您而言,您只需使用SAFE_CAST(UPDT_DT_TM AS DATETIME) It is in the Functions & Operators documentation . 它在功能和运算符文档中

SAFE_CAST(DTL.ORDER_QTY AS DECIMAL(18,4)) SAFE_CAST(DTL.ORDER_QTY AS DECIMAL(18,4))

THIS TYPES NOT ALLOWED不允许使用此类型

ONLY DECIMAL只有十进制

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM