I have the following code in BigQuery (standard SQL):
SAFE_CAST (PARSE_DATE('%Y-%m-%d',JSON_EXTRACT_SCALAR(g.p_dataforanalytics,'$.birthday') ) as string)
This fails due to:
Invalid result from parsing function
The problem is that I have no control of the data I get in g.p_dataforanalytics
some of it is pure garbage and not in the right format, sometimes it's not even reasonable value.
Is there a way to define that if PARSE_DATE()
fails it will return NULL and not throw the query out?
Basicly I'm looking for something like a try/catch in any programming language?
PARSE_DATE(format_string, date_string) Description. Converts a string representation of date to a DATE object. format_string contains the format elements that define how date_string is formatted.
BigQuery STRING_AGG function returns a value by gathering all expressions in a row and generating a concatenated string of non-null values. The value can be either STRING or BYTES. The result generally depends on the first expression taken by the BigQuery STRING_AGG function.
Other BigQuery FunctionsTRUNCATE Table. Unicode Table Naming. Create External Tables Using a DDL Statement. EXPORT DATA.
Use the SAFE.
prefix on the parse:
SAFE.PARSE_DATE('%Y-%m-%d', JSON_EXTRACT_SCALAR(g.p_dataforanalytics, '$.birthday') )
I don't see the utility of converting this back to a string, unless you wanted a solution like this:
COALESCE(FORMAT('%Y-%m-%d',
COALESCE(SAFE.PARSE_DATE('%Y-%m-%d', JSON_EXTRACT_SCALAR(g.p_dataforanalytics, '$.birthday'),
SAFE.PARSE_DATE('%m/%d/%Y', JSON_EXTRACT_SCALAR(g.p_dataforanalytics, '$.birthday')
)
), JSON_EXTRACT_SCALAR(g.p_dataforanalytics, '$.birthday'
)
That is, if the code tested different formats for the date, chose one, turned it back into YYYY-MM-DD format. And, if none of the formats worked, then kept the original value.
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