DEV Community

Saw Thawah
Saw Thawah

Posted on

How to format date from Firestore in BigQuery

Im trying to display dates from Firestore Database in BigQuery.

Here is an example of my Firestore Document๐Ÿ‘‡

Here is an example of my Firestore Document

I would like to display this timestamp, "f58-sodleDate", in BigQuery as 2023-10-3.

In BigQuery Console, I wrote this code ๐Ÿ‘‡

json_extract(data, '$.f58-sodleDate'),
Enter fullscreen mode Exit fullscreen mode

and I get this ๐Ÿ‘‰ {"_seconds":1691471596,"_nanoseconds":483000000}

And if I write this code, ๐Ÿ‘‡

parse_datetime("%c", JSON_VALUE(DATA, '$.f58-sodleDate')) as dte,

// or this code

JSON_EXTRACT_SCALAR(DATA, '$.f58-sodleDate') AS dte,
Enter fullscreen mode Exit fullscreen mode

I get the query result as null, like this๐Ÿ‘‡

this query result

And if I write this code๐Ÿ‘‡

date(timestamp_seconds(cast(json_extract(data, '$.f58-sodleDate') as int64))) AS dte,
Enter fullscreen mode Exit fullscreen mode

I get this error message๐Ÿ‘‡

error message

What I want for my query result is something like this๐Ÿ‘‡
correct query result

I achieved this by writing this code ๐Ÿ‘‡

FORMAT_DATE('%F', timestamp) as dte,
Enter fullscreen mode Exit fullscreen mode

Please tell me what I have to do to achieve the result I want..

Thank you, best. - STW

Top comments (0)