[d] + [b] moves a date-time forward by b days; a fractional number adds part of a day, so + 1.5 is 36 hours. In DuckDB, add an interval: d + to_days(b) for whole days, d + to_seconds(x * 86400) for fractions. A null on either side gives null.
Updated · Checked on the xmetric engine
What is the syntax?
date + number
date
a date or date-time.
number
days to add; may be negative or fractional.
Returns a date or date-time number days later. 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.
What is the date b days after d?
Tableau
[d]+[b]
SQL (DuckDB)
SELECT d +to_days(b) AS result
FROM input
ORDERBY id
Row 8 has b = -2, so it moves back two days.
b
d
Result
2
2024-02-2818:00
2024-03-0118:00
2
2024-02-2900:00
2024-03-0200:00
5
2024-03-0109:30
2024-03-0609:30
4
2025-01-0112:00
2025-01-0512:00
3
null
2
2024-01-0108:00
2024-01-0308:00
2024-12-3123:00
null
−2
2024-06-1510:00
2024-06-1310:00
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does to_days(CAST(x AS INTEGER)) move the time?
to_days takes whole days, and the cast rounds 1.5 to 2. Tableau adds 1.5 days, 36 hours. Add seconds instead.
Tableau
[d]+[x]
Tempting SQL
SELECT d +to_days(CAST (x AS INTEGER)) AS result
FROM input
ORDERBY id
SQL that matches
SELECT d +to_seconds(x *86400) AS result
FROM input
ORDERBY id
x
d
Tableau
Tempting SQL
SQL that matches
1.5
2024-02-2818:00
2024-03-0106:00
2024-03-0118:00
2024-03-0106:00
−2.5
2024-02-2900:00
2024-02-2612:00
2024-02-2700:00
2024-02-2612:00
0.1
2024-03-0109:30
2024-03-0111:54
2024-03-0109:30
2024-03-0111:54
2.25
2025-01-0112:00
2025-01-0318:00
2025-01-0312:00
2025-01-0318:00
null
null
null
4
2024-01-0108:00
2024-01-0508:00
2024-01-0508:00
2024-01-0508:00
−1
2024-12-3123:00
2024-12-3023:00
2024-12-3023:00
2024-12-3023:00
3
2024-06-1510:00
2024-06-1810:00
2024-06-1810:00
2024-06-1810:00
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
How do I add a fixed number of days?
A constant works the same way; a literal interval reads best in SQL. Columns 1 to 8 are the sample rows, by id.
Case
Tableau, then SQL (DuckDB)
1
2
3
4
5
6
7
8
plus 7 days
[d] + 7d + INTERVAL 7 DAY
2024-03-0618:00
2024-03-0700:00
2024-03-0809:30
2025-01-0812:00
null
2024-01-0808:00
2025-01-0723:00
2024-06-2210:00
✓ 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
SELECTDATEADD(day, b, d) AS result
FROM input
ORDERBY id