Tableau function Date

Tableau DATENAME to SQL

DATENAME(date_part, date, [start_of_week]) returns a part of a date as a string. Months and weekdays come back as names ("March", "Sunday"); every other part comes back as its number written as text, without zero padding ("1", not "01"). In DuckDB use monthname() and dayname() for the names and cast the number parts to VARCHAR; strftime pads them.

Updated · Checked on the xmetric engine

What is the syntax?

DATENAME(date_part, date, [start_of_week])
date_part
a quoted string, the same parts as DATEPART: 'year', 'month', 'weekday', 'week', 'iso-year' and so on.
date
a date or date-time.
start_of_week
optional: 'sunday' to 'saturday'. It changes 'week', not the weekday name.

Returns a string: the month or weekday name, or the part's number as text. 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.

What is the month name of each order?

Tableau

DATENAME('month', [Ordered])

SQL (DuckDB)

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

Full English names. monthname() matches; strftime(Ordered, '%B') does too.

OrderedResult
2021-01-01 08:00January
2021-01-03 18:45January
2024-02-29 10:15February
2024-12-29 16:25:40December
2024-12-30 09:05December
2025-01-31 23:59:59January
2025-03-01 00:00March
2025-03-02 12:30:15March
null

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

What is the weekday name of each order?

Tableau

DATENAME('weekday', [Ordered])

SQL (DuckDB)

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

Orders 2, 4 and 8 are Sundays, order 7 a Saturday.

OrderedResult
2021-01-01 08:00Friday
2021-01-03 18:45Sunday
2024-02-29 10:15Thursday
2024-12-29 16:25:40Sunday
2024-12-30 09:05Monday
2025-01-31 23:59:59Friday
2025-03-01 00:00Saturday
2025-03-02 12:30:15Sunday
null

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

What is the day of the month as text?

Tableau

DATENAME('day', [Ordered])

SQL (DuckDB)

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

Cast the number: 1 March gives "1".

OrderedResult
2021-01-01 08:001
2021-01-03 18:453
2024-02-29 10:1529
2024-12-29 16:25:4029
2024-12-30 09:0530
2025-01-31 23:59:5931
2025-03-01 00:001
2025-03-02 12:30:152
null

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

Where does a straight translation go wrong?

Common mistake

Why does strftime('%d') not match DATENAME('day')?

strftime pads to two digits, so 1 March is "01". DATENAME writes the number as it is: "1". The same goes for %m and %H.

Tableau

DATENAME('day', [Ordered])

Tempting SQL

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

SQL that matches

SELECT CAST (day(Ordered) AS VARCHAR) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:001011
2021-01-03 18:453033
2024-02-29 10:15292929
2024-12-29 16:25:40292929
2024-12-30 09:05303030
2025-01-31 23:59:59313131
2025-03-01 00:001011
2025-03-02 12:30:152022
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 casting month() not give DATENAME('month')?

For months, DATENAME returns the name, not the number. "1" is what STR(DATEPART('month', ...)) gives; DATENAME gives "January".

Tableau

DATENAME('month', [Ordered])

Tempting SQL

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

SQL that matches

SELECT monthname(Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:00January1January
2021-01-03 18:45January1January
2024-02-29 10:15February2February
2024-12-29 16:25:40December12December
2024-12-30 09:05December12December
2025-01-31 23:59:59January1January
2025-03-01 00:00March3March
2025-03-02 12:30:15March3March
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 strftime('%U') not match DATENAME('week')?

%U starts weeks on Sunday too, but calls the days before the first Sunday week "00", and pads. Tableau calls them week 1, so most rows are one lower.

Tableau

DATENAME('week', [Ordered])

Tempting SQL

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

SQL that matches

SELECT
  CAST (CAST (floor((dayofyear(Ordered) - 1 + dayofweek(date_trunc('year', Ordered))) / 7) + 1 AS INTEGER) AS VARCHAR) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:001001
2021-01-03 18:452012
2024-02-29 10:159089
2024-12-29 16:25:40535253
2024-12-30 09:05535253
2025-01-31 23:59:595045
2025-03-01 00:009089
2025-03-02 12:30:15100910
nullnullnull

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What does each date part return, and what is its SQL?

The same order dates with every date part. Names come from monthname() and dayname(); number parts are cast to text. Tableau weeks count from 1 January with Sunday starts. Columns 1 to 9 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)123456789
yearDATENAME('year', [Ordered])CAST(year(Ordered) AS VARCHAR)20212021202420242024202520252025null
quarterDATENAME('quarter', [Ordered])CAST(quarter(Ordered) AS VARCHAR)11144111null
monthDATENAME('month', [Ordered])monthname(Ordered)JanuaryJanuaryFebruaryDecemberDecemberJanuaryMarchMarchnull
dayDATENAME('day', [Ordered])CAST(day(Ordered) AS VARCHAR)132929303112null
dayofyearDATENAME('dayofyear', [Ordered])CAST(dayofyear(Ordered) AS VARCHAR)1360364365316061null
hourDATENAME('hour', [Ordered])CAST(hour(Ordered) AS VARCHAR)8181016923012null
minuteDATENAME('minute', [Ordered])CAST(minute(Ordered) AS VARCHAR)0451525559030null
secondDATENAME('second', [Ordered])CAST(second(Ordered) AS VARCHAR)00040059015null
week (Sunday start)DATENAME('week', [Ordered])CAST(CAST(floor((dayofyear(Ordered) - 1 + dayofweek(date_trunc('year', Ordered))) / 7) + 1 AS INTEGER) AS VARCHAR)12953535910null
weekdayDATENAME('weekday', [Ordered])dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
weekday, Sunday startDATENAME('weekday', [Ordered], 'sunday')dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
weekday, Monday startDATENAME('weekday', [Ordered], 'monday')dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
weekday, Tuesday startDATENAME('weekday', [Ordered], 'tuesday')dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
weekday, Wednesday startDATENAME('weekday', [Ordered], 'wednesday')dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
weekday, Thursday startDATENAME('weekday', [Ordered], 'thursday')dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
weekday, Friday startDATENAME('weekday', [Ordered], 'friday')dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
weekday, Saturday startDATENAME('weekday', [Ordered], 'saturday')dayname(Ordered)FridaySundayThursdaySundayMondayFridaySaturdaySundaynull
iso-yearDATENAME('iso-year', [Ordered])CAST(isoyear(Ordered) AS VARCHAR)20202020202420242025202520252025null
iso-quarterDATENAME('iso-quarter', [Ordered])CAST(CASE WHEN week(Ordered) = 53 THEN 4 ELSE CAST(floor((week(Ordered) - 1) / 13) + 1 AS INTEGER) END AS VARCHAR)44141111null
iso-weekdayDATENAME('iso-weekday', [Ordered])CAST(isodow(Ordered) AS VARCHAR)57471567null

On every row, each Tableau formula and its SQL return the same value.

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 TO_CHAR(Ordered, 'MMMM') AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT FORMAT_DATETIME('%B', Ordered) AS result
FROM input
ORDER BY id

BigQuery documentation

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

When should I use DATENAME instead of DATEPART?

When you want a label. DATENAME gives "March"; DATEPART gives 3, which sorts and computes as a number. Tableau's help notes DATEPART is faster.

Does the week start change the weekday name?

No. 2 March 2025 is "Sunday" whatever the week start; start_of_week only changes 'week' numbers. The weekday cases below all return the same names.

Why do month names sort alphabetically?

They are strings, so April comes first. Sort the field by DATEPART('month', ...), or in SQL ORDER BY month(Ordered).

Where is this documented?