HomeIntegrationsPricingLearn
Sign inGet started

Data to decisions, faster.

Learn

  • Blog
  • Webinars
  • SQL tutorials

Legal & security

  • Privacy Policy
  • Terms of Use
  • Cookies Policy
  • Trust Center
  • Security

Social

  • LinkedIn →
  • YouTube →
  • X →

Join 4,000+ data professionals who believe data is more than just building chatbots.

A weekly email to help you think through data + AI

© 2026 Count Technologies Ltd. All rights reserved.

Book a Demo →

SQL Resources

Count is the best SQL IDE, wrapped up in the best data analysis tool, all inside the best BI platform.

ARRAY_AGG
Arrays Explained
Arrays Explained
CASE
CAST
COALESCE
CONCAT
COUNT [DISTINCT]
CURRENT_DATE
CURRENT_DATETIME
CURRENT_TIMESTAMP
DATETIME_DIFF
DATETIME_TRUNC
DATE_DIFF
DATE_TRUNC
Data Types
Dates and Times
EXTRACT
IF
MEDIAN
SUBSTR
String Functions Explained
TIMESTAMP_DIFF
DefinitionPractical InfoCommon QuestionsTroubleshooting Common ErrorsRelated Pages
TIMESTAMP_TRUNC
TIME_DIFF
TIME_TRUNC
UNNEST
Window Functions Explained
SQL Resources/BigQuery Standard SQL/TIMESTAMP_DIFF

TIMESTAMP_DIFF

TIMESTAMP_DIFF function. Definition, syntax, examples and common errors using BigQuery Standard SQL. The TIMESTAMP_DIFF function allows you to find the difference between 2 timestamp objects.

ℹ️

See also DATE_DIFF, DATETIME_DIFF, or TIME_DIFF

Definition

The TIMESTAMP_DIFF function allows you to find the difference between 2 TIMESTAMPs in the specified date_part interval.

Where date_part can be any of the following:

TIMESTAMP_DIFF(timestamp_expression_a, timestamp_expression_b, date_part)
  • MICROSECOND
  • MILLISECOND

Returns: INT64

minutes_differencemillisecond_difference
9813173000

Practical Info

  • If the first TIMESTAMP is earlier than the second one then the output will be negative
  • Throws an error if the computation overflows the result type, such as if the difference in microseconds between the two TIMESTAMP objects would overflow an INT64 value.

Common Questions

How to filter for the last n seconds?

A common SQL query WHERE clause is to just pull the data for the last n seconds. In this case, we can make use of TIMESTAMP_DIFF and CURRENT_TIMESTAMP:

SELECT
  TIMESTAMP_DIFF(CAST('2021-01-01 14:01:05' AS TIMESTAMP), CAST('2021-01-01 12:22:23' AS TIMESTAMP), MINUTE) AS minutes_difference,
  TIMESTAMP_DIFF(CAST('2021-01-01 01:44:33' AS TIMESTAMP), CAST('2020-12-31 22:04:60' AS TIMESTAMP), MILLISECOND) AS millisecond_difference
dateseconds_since
2021-05-13T11:50:41.110Z30
2021-05-13T11:51:01.110Z10

Troubleshooting Common Errors

Argument 2 of TIMESTAMP_DIFF has incorrect type: expected timestamp found datetime.

This one is all too common. If you're using TIMESTAMP, you'll need to make sure both of your TIMESTAMPs are indeed TIMESTAMP data types, and not DATEs or DATETIMEs. It's usually easy enough to add a CAST(datetime_col as TIMESTAMP) to your function:

SELECT
  date,
  TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), date, SECOND) AS seconds_since
FROM
  (
    SELECT
      TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 SECOND) AS date
    UNION ALL
(    SELECT
      TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 90 SECOND) AS date)
    UNION ALL
(    SELECT
      TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 SECOND) AS date)
  ) AS table_3
WHERE
  (TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), date, SECOND) <= 30)

Related Pages

  • DATETIME_DIFF
  • Dates & Times in Standard SQL
TIMESTAMP_DIFF(CAST('2020-01-01 03:22:01' as TIMESTAMP),'2021-01-05 03:04:00', DAY) days_diff

Got a CSV?
See it differently in <2 mins

Get started for FREEWatch our CEO do it