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

BigQuery: cast string as datetime

I have a column with millions of dates called ‘timestamp’ in bigquery, but they are marked as string.

The format of the timestamp column is as follows:

2022-02-09T12:01:51.911821Z

for every observation in the dataset.

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

I wish to convert it to datetime and have tried the following:

SELECT
  CAST(timestamp AS DATETIME)
FROM
  `bigquery_table`
LIMIT
  1000

The query will process but I am told that ‘Invalid datetime string "2022-02-09T12:01:51.911821Z"’. Any suggestions to solve this?

Thanks

>Solution :

That might be ISO 8601 format, you can use PARSE_TIMESTAMP function.

SELECT
  PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%E*SZ', timestamp)
FROM
  `bigquery_table`
LIMIT
  1000
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