[d] - [e] returns the number of days between two date-times, including the fraction: 36 hours is 1.5. In DuckDB, subtract epochs and divide by 86,400. date_diff('day', e, d) answers another question, the number of midnights crossed, and rounds the fraction away.
Updated · Checked on the xmetric engine
What is the syntax?
date - date
date
two dates or date-times; the result is positive when the first is later.
Returns a number of days, fractional for date-times. Null if either side is null.
What does it return, and what is the SQL?
Each example runs on the same 8 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
How many days passed between e and d?
Tableau
[d]-[e]
SQL (DuckDB)
SELECT (epoch(d) -epoch(e)) /86400AS result
FROM input
ORDERBY id
Row 1: 36 hours is 1.5 days. Row 2: one hour is 0.04.
d
e
Result
2024-02-2818:00
2024-02-2706:00
1.5
2024-02-2900:00
2024-02-2823:00
0.04
2024-03-0109:30
2024-02-2809:30
2
2025-01-0112:00
2024-12-3100:00
1.5
2024-01-0100:00
null
2024-01-0108:00
null
2024-12-3123:00
2024-01-3100:00
336
2024-06-1510:00
2024-06-1510:00
0
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does date_diff('day') count one day for an hour?
date_diff counts midnights crossed: 23:00 to 00:00 is 1. Tableau's - measures elapsed time: 0.04 days.
Tableau
[d]-[e]
Tempting SQL
SELECTdate_diff('day', e, d) AS result
FROM input
ORDERBY id
SQL that matches
SELECT (epoch(d) -epoch(e)) /86400AS result
FROM input
ORDERBY id
d
e
Tableau
Tempting SQL
SQL that matches
2024-02-2818:00
2024-02-2706:00
1.5
1
1.5
2024-02-2900:00
2024-02-2823:00
0.04
1
0.04
2024-03-0109:30
2024-02-2809:30
2
2
2
2025-01-0112:00
2024-12-3100:00
1.5
1
1.5
2024-01-0100:00
null
null
null
2024-01-0108:00
null
null
null
2024-12-3123:00
2024-01-3100:00
336
335
336
2024-06-1510:00
2024-06-1510:00
0
0
0
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What if both sides are dates?
Converting to dates first gives whole days. Columns 1 to 8 are the sample rows, by id.
Case
Tableau, then SQL (DuckDB)
1
2
3
4
5
6
7
8
dates only
DATE([d]) - DATE([e])CAST(d AS DATE) - CAST(e AS DATE)
1
1
2
1
null
null
335
0
✓ On every row, each Tableau formula and its SQL return the same value.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECTDATEDIFF(second, e, d) /86400AS result
FROM input
ORDERBY id