Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add five hours to timestamp field

I want to add few hours eg: 5hours 30 mins to timestamp field using bigquery. My timestamp field is in the format - '2016-05-03 21:35:03'

How can i do this in bigquery?

like image 278
rbkk2016 Avatar asked Dec 04 '22 00:12

rbkk2016


2 Answers

For completeness, the equivalent standard SQL query (uncheck "Use Legacy SQL" under "Show Options") would be:

WITH T AS (
  SELECT ts
  FROM UNNEST([CURRENT_TIMESTAMP(),
               TIMESTAMP("2016-05-03 21:35:03")]) AS ts)
SELECT TIMESTAMP_ADD(ts, INTERVAL 330 MINUTE) AS ts_plus_530
FROM T;
+---------------------+
|     ts_plus_530     |
+---------------------+
| 2016-08-09 04:18:05 |
| 2016-05-04 03:05:03 |
+---------------------+

Documentation for TIMESTAMP_ADD is here: https://cloud.google.com/bigquery/sql-reference/functions-and-operators#timestamp_add

like image 126
Elliott Brossard Avatar answered Jan 07 '23 18:01

Elliott Brossard


SELECT 
  ts, 
  DATE_ADD(ts, 330, "MINUTE") AS ts_plus_530 
FROM 
  (SELECT CURRENT_TIMESTAMP() AS ts),
  (SELECT TIMESTAMP("2016-05-03 21:35:03") AS ts)

See DATE_ADD for more details

like image 37
Mikhail Berlyant Avatar answered Jan 07 '23 17:01

Mikhail Berlyant