Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to handle NULL values when using DATE or CAST functions in BigQuery

I have a string field ‘ShippedDate’ in the following format (1998-04-06 0:00:00) that I need to convert to datetime format. I have tried using DATE and CAST functions, but because there are NULL values in the field I am getting the following error: Invalid timestamp: ‘NULL’.

I tried using those functions with IFNULL (see below), but I’m still getting the same error. How do I get around this issue? Thanks!

SELECT IFNULL(CAST(ShippedDate AS DATETIME),null)

SELECT IFNULL(DATE(ShippedDate),null)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use SAFE_CAST instead.

SELECT SAFE_CAST(ShippedDate AS DATETIME);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading