HomeIntegrationsPricingLearn
Sign inGet started

Data to decisions, faster.

Book a Demo

Learn

  • Blog
  • Webinars
  • SQL tutorials

Legal & security

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

© 2026 Count Technologies Ltd. All rights reserved.

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
DefinitionPractical InfoCommon QuestionsTroubleshooting Common ErrorsRelated Pages
DATETIME_TRUNC
DATE_DIFF
DATE_TRUNC
Data Types
Dates and Times
EXTRACT
IF
MEDIAN
SUBSTR
String Functions Explained
TIMESTAMP_DIFF
TIMESTAMP_TRUNC
TIME_DIFF
TIME_TRUNC
UNNEST
Window Functions Explained
SQL Resources/BigQuery Standard SQL/DATETIME_DIFF

DATETIME_DIFF

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

ℹ️

You might also want to see DATE_DIFF, TIMESTAMP_DIFF, OR TIME_DIFF

Definition

The DATETIME_DIFF function in BigQuery allows you to find the difference between 2 DATETIMEs in the specified date_part interval.

Where date_part can be any of the following:

DATETIME_DIFF(datetime_expression_a, datetime_expression_b, date_part)
  • MICROSECOND
  • MILLISECOND
  • WEEK : Begins on Sunday
  • WEEK(): Begins on  where WEEKDAY can be SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
  • ISOWEEK: Uses ISO 8601 week boundaries. ISO weeks begin on Monday.
  • ISOYEAR: Uses the ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.

Returns: INT64

minutes_differencequarter_difference
993

Practical Info

  • If the first DATETIME 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 DATETIME objects would overflow an INT64 value.

Common Questions

How to filter for the last n days?

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

SELECT
  DATETIME_DIFF(CAST('2021-01-01 14:01:05' AS DATETIME), CAST('2021-01-01 12:22:23' AS DATETIME), MINUTE) AS minutes_difference,
  DATETIME_DIFF(CAST('2021-01-01 04:44:33' AS DATETIME), CAST('2020-06-22' AS DATETIME), QUARTER) AS quarter_difference
datedays_since
2021-01-22T00:00:0021
2021-01-26T00:00:0017

Troubleshooting Common Errors

Argument 2 of DATETIME_DIFF has incorrect type: expected datetime found date.

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

SELECT
  date,
  DATETIME_DIFF(CURRENT_DATETIME(), date, DAY) AS days_since
FROM
  (
    SELECT
      CAST('2021-01-22' AS DATETIME) AS date
    UNION ALL
(    SELECT
      CAST('2020-01-25' AS DATETIME) AS date)
    UNION ALL
(    SELECT
      CAST('2021-01-26' AS DATETIME) AS date)
  ) AS table_3
WHERE
  (DATETIME_DIFF(CURRENT_DATETIME(), date, DAY) <= 30)

Related Pages

  • TIMESTAMP_DIFF
  • Dates & Times in Standard SQL
DATETIME_DIFF(CAST('2020-01-01' as DATETIME),'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