Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bigquery - integer to timestamp

I have an integer 20160115201307 and I want to convert it as the timestamp 2016-01-15 20:13:07 UTC. I have tried timestamp(date) but it gave wrong date as 1970-08-22 08:01:55 UTC. Anyone suggest which query I need to use?

like image 707
nikhil Avatar asked Mar 13 '26 01:03

nikhil


1 Answers

That is more likely a string not an integer, and you need to use regular expression to parse the string into components.

SELECT ds, 
   TIMESTAMP(REGEXP_REPLACE(ds, r'(....)(..)(..)(..)(..)(..)', r'\1-\2-\3 \4:\5:\6')) ts
FROM (SELECT '20160115201307 ' ds)

returns

+-----+----------------+-------------------------+--+
| Row | ds             | ts                      |  |
+-----+----------------+-------------------------+--+
| 1   | 20160115201307 | 2016-01-15 20:13:07 UTC |  |
+-----+----------------+-------------------------+--+
like image 172
Pentium10 Avatar answered Mar 14 '26 13:03

Pentium10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!