Tableau function Date

Tableau MONTH to SQL

MONTH(date) returns the month number, 1 to 12, as an integer: MONTH(#2025-01-31#) is 1. It equals DATEPART('month', date). In DuckDB use month(). A strftime('%m') translation returns the text '01', not the number 1.

Updated · Checked on the xmetric engine

What is the syntax?

MONTH(date)
date
a date or date-time.

Returns an integer from 1 to 12. 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 month was each order placed in?

Tableau

MONTH([Ordered])

SQL (DuckDB)

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

Order 6 at 23:59:59 on 31 January is still month 1; order 7 at midnight on 1 March is month 3.

OrderedResult
2021-01-01 08:001
2021-01-03 18:451
2024-02-29 10:152
2024-12-29 16:25:4012
2024-12-30 09:0512
2025-01-31 23:59:591
2025-03-01 00:003
2025-03-02 12:30:153
null

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

How do I build a year-month key such as 202501?

Tableau

YEAR([Ordered]) * 100 + MONTH([Ordered])

SQL (DuckDB)

SELECT year(Ordered) * 100 + month(Ordered) AS result
FROM input
ORDER BY id

An integer key that sorts in time order and keeps years apart.

OrderedResult
2021-01-01 08:00202,101
2021-01-03 18:45202,101
2024-02-29 10:15202,402
2024-12-29 16:25:40202,412
2024-12-30 09:05202,412
2025-01-31 23:59:59202,501
2025-03-01 00:00202,503
2025-03-02 12:30:15202,503
null

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

Where does a straight translation go wrong?

Common mistake

Why does strftime('%m') not match MONTH?

strftime returns text padded to two digits, so January is '01', not the number 1. Comparisons with numbers and sums break. MONTH is an integer: use month().

Tableau

MONTH([Ordered])

Tempting SQL

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

SQL that matches

SELECT month(Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:001011
2021-01-03 18:451011
2024-02-29 10:152022
2024-12-29 16:25:40121212
2024-12-30 09:05121212
2025-01-31 23:59:591011
2025-03-01 00:003033
2025-03-02 12:30:153033
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 MONTH(Ordered) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

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

How do I get the month name instead of the number?

Use DATENAME('month', [Ordered]), which returns "January". In DuckDB that is monthname(Ordered).

Does MONTH keep the year apart?

No. January 2021 and January 2025 both return 1. To group by month and year, use DATETRUNC('month', [Ordered]) (SQL: date_trunc('month', Ordered)).

What does MONTH return for a null date?

Null, in Tableau and in SQL.

Where is this documented?