简体   繁体   中英

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.

The DATETIME fields are corrupted with values like "None" and "0.0" which causes our CAST statement to fail.

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.

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?

You can use the SAFE_CAST function, which returns NULL if the input is not a valid value when interpreted as the desired type. In your case, you would just use SAFE_CAST(UPDT_DT_TM AS DATETIME) . It is in the Functions & Operators documentation .

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

THIS TYPES NOT ALLOWED

ONLY DECIMAL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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