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)
SELECTmonth(Ordered) AS result
FROM input
ORDERBY id
Order 6 at 23:59:59 on 31 January is still month 1; order 7 at midnight on 1 March is month 3.
Ordered
Result
2021-01-0108:00
1
2021-01-0318:45
1
2024-02-2910:15
2
2024-12-2916:25:40
12
2024-12-3009:05
12
2025-01-3123:59:59
1
2025-03-0100:00
3
2025-03-0212:30:15
3
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)
SELECTyear(Ordered) *100+month(Ordered) AS result
FROM input
ORDERBY id
An integer key that sorts in time order and keeps years apart.
Ordered
Result
2021-01-0108:00
202,101
2021-01-0318:45
202,101
2024-02-2910:15
202,402
2024-12-2916:25:40
202,412
2024-12-3009:05
202,412
2025-01-3123:59:59
202,501
2025-03-0100:00
202,503
2025-03-0212:30:15
202,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
SELECTstrftime(Ordered, '%m') AS result
FROM input
ORDERBY id
SQL that matches
SELECTmonth(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
1
01
1
2024-02-2910:15
2
02
2
2024-12-2916:25:40
12
12
12
2024-12-3009:05
12
12
12
2025-01-3123:59:59
1
01
1
2025-03-0100:00
3
03
3
2025-03-0212:30:15
3
03
3
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
SELECTMONTH(Ordered) AS result
FROM input
ORDERBY id