Tableau function Date

Tableau DATE to SQL

DATE(expression) returns a date. Given a date-time it drops the time; given a string it recognises many common formats, such as "2021-01-01", "January 3, 2021" and, in a US English locale, "12/30/2024". Text that isn't a valid date returns null. DuckDB's CAST(... AS DATE) reads only ISO dates, so the matching SQL tries each format in turn.

Updated · Checked on the xmetric engine

What is the syntax?

DATE(expression)
expression
a string, a date-time or a date.

Returns a date. Null if the input is null or isn't a recognisable date.

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.

What is the date of each order, without the time?

Tableau

DATE([Ordered])

SQL (DuckDB)

SELECT CAST (Ordered AS DATE) AS result
FROM input
ORDER BY id

The time is dropped, not rounded: 23:59:59 stays on the same day.

OrderedResult
2021-01-01 08:002021-01-01
2021-01-03 18:452021-01-03
2024-02-29 10:152024-02-29
2024-12-29 16:25:402024-12-29
2024-12-30 09:052024-12-30
2025-01-31 23:59:592025-01-31
2025-03-01 00:002025-03-01
2025-03-02 12:30:152025-03-02
null

The Tableau formula and the SQL return the same 9 rows.

Which dates do the text values hold?

Tableau

DATE([Text])

SQL (DuckDB)

SELECT
  coalesce(try_cast(Text AS DATE), CAST (try_strptime(Text, '%B %d, %Y') AS DATE), CAST (try_strptime(Text, '%m/%d/%Y') AS DATE)) AS result
FROM input
ORDER BY id

Try ISO first, then "Month day, year", then US month/day/year. Row 3 has a time, which is dropped; rows 7 and 8 aren't dates and return null.

TextResult
2021-01-012021-01-01
January 3, 20212021-01-03
2024-02-29 10:152024-02-29
2024-12-292024-12-29
12/30/20242024-12-30
2025-01-312025-01-31
2025-02-29null
n/anull
null

The Tableau formula and the SQL return the same 9 rows.

Where does a straight translation go wrong?

Common mistake

Why does try_cast(Text AS DATE) miss rows that Tableau reads?

DuckDB only casts ISO text such as 2021-01-03. Tableau also reads "January 3, 2021" and "12/30/2024", so rows 2 and 5 are dates in Tableau and null in the cast.

Tableau

DATE([Text])

Tempting SQL

SELECT try_cast(Text AS DATE) AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  coalesce(try_cast(Text AS DATE), CAST (try_strptime(Text, '%B %d, %Y') AS DATE), CAST (try_strptime(Text, '%m/%d/%Y') AS DATE)) AS result
FROM input
ORDER BY id
TextTableauTempting SQLSQL that matches
2021-01-012021-01-012021-01-012021-01-01
January 3, 20212021-01-03null2021-01-03
2024-02-29 10:152024-02-292024-02-292024-02-29
2024-12-292024-12-292024-12-292024-12-29
12/30/20242024-12-30null2024-12-30
2025-01-312025-01-312025-01-312025-01-31
2025-02-29nullnullnull
n/anullnullnull
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 date_trunc('day') not give a date?

DuckDB's date_trunc on a timestamp returns a timestamp at midnight, so the type is still a date-time. DATE returns a date; cast to DATE instead.

Tableau

DATE([Ordered])

Tempting SQL

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

SQL that matches

SELECT CAST (Ordered AS DATE) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:002021-01-012021-01-01 00:002021-01-01
2021-01-03 18:452021-01-032021-01-03 00:002021-01-03
2024-02-29 10:152024-02-292024-02-29 00:002024-02-29
2024-12-29 16:25:402024-12-292024-12-29 00:002024-12-29
2024-12-30 09:052024-12-302024-12-30 00:002024-12-30
2025-01-31 23:59:592025-01-312025-01-31 00:002025-01-31
2025-03-01 00:002025-03-012025-03-01 00:002025-03-01
2025-03-02 12:30:152025-03-022025-03-02 00:002025-03-02
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 TRY_TO_DATE(Text) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT SAFE_CAST(Text AS DATE) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own DATE 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 DATE?

What does DATE return for text that isn't a date?

Null. "n/a" and 29 February 2025 (not a leap year) both return null; use ISDATE to test first.

When should I use DATEPARSE instead?

When the format isn't one Tableau recognises, or is ambiguous, such as day-first "29/12/2024" in a US locale. DATEPARSE takes the exact format.

Does DATE round a date-time?

No, it truncates. Order 6 at 23:59:59 on 31 January stays 31 January.

Where is this documented?