DAY(date) returns the day of the month, 1 to 31, as an integer: DAY(#2024-02-29#) is 29. It equals DATEPART('day', date). In DuckDB use day(). Don't confuse it with the day of the year, which is DATEPART('dayofyear', ...).
Updated · Checked on the xmetric engine
What is the syntax?
DAY(date)
date
a date or date-time.
Returns an integer from 1 to 31. Null if the date is null.
What does it return, and what is the SQL?
Each example runs on the same 9 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
Which day of the month was each order placed on?
Tableau
DAY([Ordered])
SQL (DuckDB)
SELECTday(Ordered) AS result
FROM input
ORDERBY id
The leap day, order 3, returns 29.
Ordered
Result
2021-01-0108:00
1
2021-01-0318:45
3
2024-02-2910:15
29
2024-12-2916:25:40
29
2024-12-3009:05
30
2025-01-3123:59:59
31
2025-03-0100:00
1
2025-03-0212:30:15
2
null
✓ The Tableau formula and the SQL return the same 9 rows.
Was the order placed on the last day of its month?
Tableau
DAY(DATEADD('day', 1, [Ordered])) =1
SQL (DuckDB)
SELECTday(Ordered +INTERVAL1 DAY) =1AS result
FROM input
ORDERBY id
The next day is the 1st only at a month end: orders 3 (29 February 2024) and 6 (31 January 2025) are true.
Ordered
Result
2021-01-0108:00
false
2021-01-0318:45
false
2024-02-2910:15
true
2024-12-2916:25:40
false
2024-12-3009:05
false
2025-01-3123:59:59
true
2025-03-0100:00
false
2025-03-0212:30:15
false
null
✓ The Tableau formula and the SQL return the same 9 rows.
Where does a straight translation go wrong?
Common mistake
Why does dayofyear() disagree with DAY after January?
DAY is the day of the month. dayofyear() keeps counting through the year, so 29 February 2024 is 60, not 29. They only agree in January.
Tableau
DAY([Ordered])
Tempting SQL
SELECTdayofyear(Ordered) AS result
FROM input
ORDERBY id
SQL that matches
SELECTday(Ordered) AS result
FROM input
ORDERBY id
Ordered
Tableau
Tempting SQL
SQL that matches
2021-01-0108:00
1
1
1
2021-01-0318:45
3
3
3
2024-02-2910:15
29
60
29
2024-12-2916:25:40
29
364
29
2024-12-3009:05
30
365
30
2025-01-3123:59:59
31
31
31
2025-03-0100:00
1
60
1
2025-03-0212:30:15
2
61
2
null
null
null
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why does strftime('%d') not match DAY?
strftime returns two-digit text: 1 March is '01'. DAY is the integer 1.
Tableau
DAY([Ordered])
Tempting SQL
SELECTstrftime(Ordered, '%d') AS result
FROM input
ORDERBY id
SQL that matches
SELECTday(Ordered) AS result
FROM input
ORDERBY id
Ordered
Tableau
Tempting SQL
SQL that matches
2021-01-0108:00
1
01
1
2021-01-0318:45
3
03
3
2024-02-2910:15
29
29
29
2024-12-2916:25:40
29
29
29
2024-12-3009:05
30
30
30
2025-01-3123:59:59
31
31
31
2025-03-0100:00
1
01
1
2025-03-0212:30:15
2
02
2
null
null
null
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
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
SELECTDAY(Ordered) AS result
FROM input
ORDERBY id