DATEDIFF()

SQL · Date Functions

Theory

DATEDIFF tells you how far apart two dates are, as a count of a unit such as days, months or years. If an order was placed on 2026-11-02 and shipped on 2026-11-05, the difference is 3 days. It answers questions like 'how many days did delivery take?', 'how long has each employee worked here?' or 'how overdue is this invoice?'.

Every database spells it differently, and argument order is the classic trap. SQL Server and Snowflake use DATEDIFF(day, start, end) and count unit boundaries crossed, so DATEDIFF(year, '2025-12-31', '2026-01-01') is 1 even though only one day passed. MySQL uses DATEDIFF(end, start) and returns days only. PostgreSQL subtracts dates directly: end_date - start_date gives days. SQLite has no DATEDIFF; you subtract julianday() values and cast the result to an integer. A negative result usually means the arguments are reversed.

Why we use it

Durations drive many business metrics: delivery time, customer age, days overdue, time between visits. DATEDIFF converts two dates into one number you can average, filter or bucket.

When to use it

Use it whenever you need elapsed time between two dates. To produce a new date (start + 30 days) use DATE_ADD instead. For exact ages in years, check how your database counts boundaries, or compare full dates.

How it works

  1. Two dates: start and end
  2. Choose the unit: day, month, year
  3. Unit-first dialect (SQL Server/Snowflake)? Yes: DATEDIFF(day, start, end). No: MySQL: DATEDIFF(end, start).
  4. Database counts units between them
  5. Is the result negative? Yes: Arguments are probably reversed. No: Use the number.
  6. Duration ready to average or filter

Syntax

DATEDIFF(date1, date2)

Example

SELECT DATEDIFF(CURRENT_DATE, hire_date) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic