Tableau function Date

Tableau DAY to SQL

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)

SELECT day(Ordered) AS result
FROM input
ORDER BY id

The leap day, order 3, returns 29.

OrderedResult
2021-01-01 08:001
2021-01-03 18:453
2024-02-29 10:1529
2024-12-29 16:25:4029
2024-12-30 09:0530
2025-01-31 23:59:5931
2025-03-01 00:001
2025-03-02 12:30:152
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)

SELECT day(Ordered + INTERVAL 1 DAY) = 1 AS result
FROM input
ORDER BY id

The next day is the 1st only at a month end: orders 3 (29 February 2024) and 6 (31 January 2025) are true.

OrderedResult
2021-01-01 08:00false
2021-01-03 18:45false
2024-02-29 10:15true
2024-12-29 16:25:40false
2024-12-30 09:05false
2025-01-31 23:59:59true
2025-03-01 00:00false
2025-03-02 12:30:15false
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

SELECT dayofyear(Ordered) AS result
FROM input
ORDER BY id

SQL that matches

SELECT day(Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:00111
2021-01-03 18:45333
2024-02-29 10:15296029
2024-12-29 16:25:402936429
2024-12-30 09:053036530
2025-01-31 23:59:59313131
2025-03-01 00:001601
2025-03-02 12:30:152612
nullnullnull

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

SELECT strftime(Ordered, '%d') AS result
FROM input
ORDER BY id

SQL that matches

SELECT day(Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:001011
2021-01-03 18:453033
2024-02-29 10:15292929
2024-12-29 16:25:40292929
2024-12-30 09:05303030
2025-01-31 23:59:59313131
2025-03-01 00:001011
2025-03-02 12:30:152022
nullnullnull

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

SELECT DAY(Ordered) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT EXTRACT(DAY FROM Ordered) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own DAY formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Ordered], [Due], [Text], [Local], [Y], [M], [D].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about DAY?

Does DAY work on a date-time?

Yes. It ignores the time: order 6 at 23:59:59 on 31 January returns 31.

How do I get the day of the year?

DATEPART('dayofyear', [Ordered]); in DuckDB dayofyear(Ordered). 29 February 2024 is day 60.

What does DAY return for a null date?

Null, in Tableau and in SQL.

Where is this documented?