QUARTER(date) returns the calendar quarter as an integer: January to March is 1, October to December is 4. It equals DATEPART('quarter', date). In DuckDB use quarter(). Hand-rolled arithmetic on the month is easy to get one off.
Updated · Checked on the xmetric engine
What is the syntax?
QUARTER(date)
date
a date or date-time.
Returns an integer from 1 to 4. 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 quarter was each order placed in?
Tableau
QUARTER([Ordered])
SQL (DuckDB)
SELECTquarter(Ordered) AS result
FROM input
ORDERBY id
The December orders are quarter 4; the March orders are quarter 1.
Ordered
Result
2021-01-0108:00
1
2021-01-0318:45
1
2024-02-2910:15
1
2024-12-2916:25:40
4
2024-12-3009:05
4
2025-01-3123:59:59
1
2025-03-0100:00
1
2025-03-0212:30:15
1
null
✓ The Tableau formula and the SQL return the same 9 rows.
On which date does each order's quarter start?
Tableau
DATETRUNC('quarter', [Ordered])
SQL (DuckDB)
SELECTdate_trunc('quarter', Ordered) AS result
FROM input
ORDERBY id
Use DATETRUNC when you need a date, QUARTER when you need the number.
Ordered
Result
2021-01-0108:00
2021-01-0100:00
2021-01-0318:45
2021-01-0100:00
2024-02-2910:15
2024-01-0100:00
2024-12-2916:25:40
2024-10-0100:00
2024-12-3009:05
2024-10-0100:00
2025-01-3123:59:59
2025-01-0100:00
2025-03-0100:00
2025-01-0100:00
2025-03-0212:30:15
2025-01-0100:00
null
✓ The Tableau formula and the SQL return the same 9 rows.
Where does a straight translation go wrong?
Common mistake
Why does floor(month / 3) + 1 give quarter 5 in December?
Dividing the month by 3 is off by one at every quarter end: March gives 2 and December 5. The month must be shifted to start at 0 first. Use quarter().
Tableau
QUARTER([Ordered])
Tempting SQL
SELECTfloor(month(Ordered) /3) +1AS result
FROM input
ORDERBY id
SQL that matches
SELECTquarter(Ordered) AS result
FROM input
ORDERBY id
Ordered
Tableau
Tempting SQL
SQL that matches
2021-01-0108:00
1
1
1
2021-01-0318:45
1
1
1
2024-02-2910:15
1
1
1
2024-12-2916:25:40
4
5
4
2024-12-3009:05
4
5
4
2025-01-3123:59:59
1
1
1
2025-03-0100:00
1
2
1
2025-03-0212:30:15
1
2
1
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
SELECTQUARTER(Ordered) AS result
FROM input
ORDERBY id
QUARTER uses calendar months. ISOQUARTER groups ISO weeks 1-13, 14-26, 27-39 and 40-53, so 1 January 2021 (ISO week 53 of 2020) is quarter 1 by QUARTER and 4 by ISOQUARTER.
How do I label quarters like 2025 Q1?
STR(YEAR([Ordered])) + " Q" + STR(QUARTER([Ordered])); in SQL CAST(year(Ordered) AS VARCHAR) || ' Q' || CAST(quarter(Ordered) AS VARCHAR).