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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With