Skip to content
Advertisement

Calculating timestamp difference in seconds in BQ

I am trying to calculate the timestamp difference between install_time and attributed_time, in seconds. However, I keep receiving this error: No matching signature for function TIMESTAMP_DIFF for argument types: TIMESTAMP, STRING, DATE_TIME_PART. Supported signature: TIMESTAMP_DIFF(TIMESTAMP, TIMESTAMP, DATE_TIME_PART) at [10:3]

My sample data and query are below.

Sample data

Attributed_time install_time user_id
2021-02-20 18:42:31 2021-02-20 18:55:06 UTC A
2021-02-20 11:10:53 2021-02-20 11:13:39 UTC B
SELECT
  user_id,
  attributed_touch_time,
  install_time,
  TIMESTAMP_DIFF(install_time,attributed_touch_time, second) as diff_sec
FROM
  `datasource`

Can anyone point me in the right direction? Any help is appreciated. Thanks!

Advertisement

Answer

I think this is the syntax you need to use.

SELECT
  user_id,
  attributed_touch_time,
  install_time,
  TIMESTAMP_DIFF(TIMESTAMP CAST(install_time AS TIMESTAMP), TIMESTAMP CAST(attributed_touch_time AS TIMESTAMP), SECOND) as diff_sec
FROM
  `datasource`

For more info, see here.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement