Tableau function Type conversion

Tableau DATETIME to SQL

DATETIME(expression) returns a date-time. Given a date it adds midnight; given a string it recognises many common formats, such as "2024-02-29 10:15", "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 TIMESTAMP) reads only ISO text, so the matching SQL tries each format in turn.

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a date-time. 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.

Which date-times do the text values hold?

Tableau

DATETIME([Text])

SQL (DuckDB)

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

Row 3 keeps its 10:15; the other dates get midnight. Rows 7 and 8 aren't dates and return null.

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

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

What is each due date as a date-time?

Tableau

DATETIME([Due])

SQL (DuckDB)

SELECT CAST (Due AS TIMESTAMP) AS result
FROM input
ORDER BY id

A date becomes midnight of that day.

DueResult
2021-01-012021-01-01 00:00
2021-01-032021-01-03 00:00
2024-02-292024-02-29 00:00
2024-12-292024-12-29 00:00
2024-12-302024-12-30 00:00
2025-01-312025-01-31 00:00
2025-03-012025-03-01 00:00
2025-03-022025-03-02 00:00
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 TIMESTAMP) miss rows that Tableau reads?

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

Tableau

DATETIME([Text])

Tempting SQL

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

SQL that matches

SELECT
  coalesce(try_cast(Text AS TIMESTAMP), try_strptime(Text, '%B %d, %Y'), try_strptime(Text, '%m/%d/%Y')) AS result
FROM input
ORDER BY id
TextTableauTempting SQLSQL that matches
2021-01-012021-01-01 00:002021-01-01 00:002021-01-01 00:00
January 3, 20212021-01-03 00:00null2021-01-03 00:00
2024-02-29 10:152024-02-29 10:152024-02-29 10:152024-02-29 10:15
2024-12-292024-12-29 00:002024-12-29 00:002024-12-29 00:00
12/30/20242024-12-30 00:00null2024-12-30 00:00
2025-01-312025-01-31 00:002025-01-31 00:002025-01-31 00:00
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.

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_TIMESTAMP(Text) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

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

What time does DATETIME give a plain date?

Midnight: DATETIME([Due]) for 31 January 2025 is 2025-01-31 00:00:00.

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

Null. "n/a" and 29 February 2025 return null.

When should I use DATEPARSE instead?

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

Where is this documented?