Date formatting and parsing functions
Use these functions to format a date or timestamp as a string, or to parse a string into a timestamp, with format specifiers such as %Y and %m. Parsing can fail the query or return NULL when a string does not match the format.
strftime function
Returns value as a VARCHAR laid out according to format. Each format specifier, a % followed by a letter, is replaced by a part of the value. For example, %Y is the four-digit year, %m the two-digit month, and %d the day of the month. Similarly, %H is the hour from 00 to 23, %M the minute, and %S the second. The function copies other characters to the result unchanged, and %% writes a percent sign. If either argument is NULL, the result is NULL.
Other common specifiers are %y (two-digit year), %B and %b (full and abbreviated month name), and %A and %a (full and abbreviated weekday name). You can also use %j (day of the year) and %I (hour from 01 to 12) with %p (AM or PM). For fractions of a second, use %g (milliseconds) and %f (microseconds). For the time zone, use %z (UTC offset) and %Z (time zone name). A minus sign after the percent sign, as in %-d or %-m, removes the leading zero.
You can pass the arguments in either order: strftime( and value, format)strftime( return the same result. To parse a string into a timestamp with the same specifiers, use the strptime function.format, value)
Syntax
strftime(value, format)
strftime(format, value)
Arguments
The strftime function takes the following arguments:
value-
The value to format, as a
DATE,TIMESTAMP, orTIMESTAMP WITH TIME ZONEvalue. ADATEis formatted as midnight at the start of that day. format-
The format, as a string of format specifiers and literal text, such as
'%Y-%m-%d %H:%M:%S'. It must be a constantVARCHARvalue. You cannot read the format from a column.
Return type
VARCHAR
Examples
The following SQL query formats a date with the abbreviated weekday name, the day of the month without a leading zero, and the full month name.
SELECT strftime( DATE '1992-01-01', '%a, %-d %B %Y' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query formats a timestamp with milliseconds. %g writes three digits and drops the rest of the fraction.
SELECT strftime( TIMESTAMP '2021-03-04 05:06:07.123456', '%Y-%m-%d %H:%M:%S.%g' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows the difference between %m, the month, and %M, the minute.
SELECT strftime( TIMESTAMP '2021-03-04 05:06:07', '%m' ) AS month_specifier, strftime( TIMESTAMP '2021-03-04 05:06:07', '%M' ) AS minute_specifier
The query returns the following result.
| month_specifier | minute_specifier |
|---|---|
|
|
The following SQL query passes a Java-style pattern. It has no format specifiers, so the function returns the pattern as literal text instead of the date.
SELECT strftime( TIMESTAMP '2021-03-04 05:06:07', 'yyyy-MM-dd' ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
Format specifiers are case-sensitive. %m is the month and %M is the minute.
A % followed by a letter that is not a format specifier, such as %i or %Q, makes the query fail. A pattern without % signs, such as yyyy-MM-dd, does not fail: it is copied to the result as literal text.
Pass value as a typed value. When both arguments are string literals, as in strftime('2021-03-04', '%Y'), the query fails. Write DATE '2021-03-04' or cast the string first.
A TIMESTAMP WITH TIME ZONE value is formatted in the session time zone, which is UTC. To format the local time in another zone, convert the value first, as in strftime(timezone('America/New_York', .value), '%Y-%m-%d %H:%M')
Differences from Athena engine version 3
Athena engine version 3 formats timestamps with date_format, which DuckDB does not have. Use strftime, and rewrite the minutes specifier %i as %M and the seconds specifier %s as %S. For example, rewrite date_format( as value, '%Y-%m-%d %H:%i:%s')strftime(.value, '%Y-%m-%d %H:%M:%S')
In the date_format function of Athena engine version 3, %M is the month name. In strftime, %M is the minute, so a format that you copy unchanged returns minutes instead of month names. Use %B for the full month name.
Athena engine version 3 also has format_datetime, which takes a Java-style pattern such as yyyy-MM-dd HH:mm. Rewrite the pattern with format specifiers, for example strftime(. value, '%Y-%m-%d %H:%M')strftime copies a Java-style pattern to the result as literal text.
In Athena engine version 3, timestamp columns of Hive tables have millisecond precision. Casting one to VARCHAR always shows three digits of milliseconds, such as 2021-03-04 05:06:07.000. In DuckDB, the cast leaves out a zero fraction and shows up to six digits. To get the Athena engine version 3 text, use strftime(.value, '%Y-%m-%d %H:%M:%S.%g')
strptime function
Reads string according to format and returns the timestamp that it describes. The format uses the same format specifiers as strftime, for example '%Y-%m-%d %H:%M:%S'. Parts that the format does not include get a default value: the month and day become 1, the time fields become 0, and the year becomes 1900. So a format with only a date returns midnight of that day, and a format with only a time returns that time on January 1, 1900. If either argument is NULL, the result is NULL.
To accept more than one layout, pass a list of formats. The function tries them in list order and uses the first one that matches.
If string does not match the format, or no format in the list matches, the query fails. To return NULL for those strings instead, use try_strptime.
Syntax
strptime(string, format)
strptime(string, format_list)
Arguments
The strptime function takes the following arguments:
string-
The string to parse, as a
VARCHARvalue. format-
The format to read
stringwith, such as'%Y-%m-%d'. It must be a constantVARCHARformat string. format_list-
The formats to try, in list order, such as
['%d/%m/%Y', '%m/%d/%Y']. It must be a constant list ofVARCHARformat strings.
Return type
TIMESTAMP. If the format has a time zone specifier, %z or %Z, the result is TIMESTAMP WITH TIME ZONE.
Examples
The following SQL query parses a date and time that has a weekday name, a month name, and a 12-hour clock time.
SELECT strptime( 'Wed, 1 January 1992 - 08:38:40 PM', '%a, %-d %B %Y - %I:%M:%S %p' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query parses a date. The result is a TIMESTAMP at midnight, so the second column casts it to DATE.
SELECT strptime('2024-01-02', '%Y-%m-%d') AS parsed, CAST( strptime('2024-01-02', '%Y-%m-%d') AS DATE ) AS parsed_date
The query returns the following result.
| parsed | parsed_date |
|---|---|
|
|
The following SQL query passes two formats. The string does not match the first format, because 15 is not a month, so the function uses the second.
SELECT strptime( '4/15/2023 10:56:00', ['%d/%m/%Y %H:%M:%S', '%m/%d/%Y %H:%M:%S'] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query parses a UTC offset with %z. The result is a TIMESTAMP WITH TIME ZONE value, shown in UTC.
SELECT strptime( '2024-01-02 10:00:00 +0200', '%Y-%m-%d %H:%M:%S %z' ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The format must be a constant. You cannot read the format from a column.
When a string can match more than one format in the list, the order decides the result. For '03/04/2021', the list ['%d/%m/%Y', '%m/%d/%Y'] returns April 3.
To get a DATE, cast the result, as in CAST(strptime(.string, '%Y-%m-%d') AS DATE)
A result of type TIMESTAMP WITH TIME ZONE is shown in the session time zone, which is UTC.
Differences from Athena engine version 3
Athena engine version 3 parses strings with date_parse, which DuckDB does not have. Use strptime, and rewrite the minutes specifier %i as %M and the seconds specifier %s as %S. For example, rewrite date_parse('2021-03-04 05:06', '%Y-%m-%d %H:%i') as strptime('2021-03-04 05:06', '%Y-%m-%d %H:%M').
In Athena engine version 3, to_timestamp( parses a string with a pattern such as string, pattern)'yyyy-mm-dd'. In DuckDB, use strptime with format specifiers instead, for example strptime('2024-01-02', '%Y-%m-%d').
In Athena engine version 3, parse_datetime takes a Java-style pattern and returns a TIMESTAMP WITH TIME ZONE value. strptime takes format specifiers and returns a TIMESTAMP unless the format has %z or %Z. For example, rewrite parse_datetime('2021-03-04 05:06', 'yyyy-MM-dd HH:mm') as strptime('2021-03-04 05:06', '%Y-%m-%d %H:%M').
try_strptime function
Parses string by using the format specifiers in format, such as %Y for a four-digit year, %m for a month number, and %d for a day, and returns the timestamp. If string does not match the format, or names a date or time that does not exist, such as February 30, the function returns NULL instead of failing the query. If string, format, or format_list is NULL, the result is NULL.
To accept more than one layout, pass a list of formats. The function tries them in list order and uses the first one that matches.
try_strptime works like strptime, except that strptime fails the query when string does not match.
Syntax
try_strptime(string, format)
try_strptime(string, format_list)
Arguments
The try_strptime function takes the following arguments:
string-
The string to parse, as a
VARCHARvalue. format-
The format to read
stringwith. It must be a constantVARCHARformat string. For example,'%Y-%m-%d %H:%M:%S'matches'2026-11-18 10:30:00'. format_list-
The formats to try, in list order, such as
['%Y-%m-%d', '%d/%m/%Y']. It must be a constant list ofVARCHARformat strings.
Return type
TIMESTAMP. If the format has a time zone specifier, %z or %Z, the result is TIMESTAMP WITH TIME ZONE.
Examples
The following SQL query parses a date and time that has a day name, a month name, and a 12-hour clock time.
SELECT try_strptime( 'Wed, 1 January 1992 - 08:38:40 PM', '%a, %-d %B %Y - %I:%M:%S %p' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns NULL because February 30 does not exist.
SELECT try_strptime('2026-02-30', '%Y-%m-%d') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes two formats. The string does not match the first format, so the function uses the second.
SELECT try_strptime( '18/11/2026', ['%Y-%m-%d', '%d/%m/%Y'] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query parses a UTC offset with %z. The result is a TIMESTAMP WITH TIME ZONE, shown in UTC.
SELECT try_strptime( '2026-11-18 10:00:00+02', '%Y-%m-%d %H:%M:%S%z' ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
An invalid format string still fails the query. A string that does not match the format returns NULL. For example, %i is not a format specifier, so a format such as '%Y-%m-%d %H:%i:%s' makes the query fail. Use %M for minutes and %S for seconds.
The format must be a constant. You cannot read the format from a column.
When a string can match more than one format in the list, the order decides the result. For '01/02/2026', the list ['%d/%m/%Y', '%m/%d/%Y'] returns February 1, and ['%m/%d/%Y', '%d/%m/%Y'] returns January 2.
To get a DATE, cast the result, as in CAST(try_strptime(.string, '%Y-%m-%d') AS DATE)
Differences from Athena engine version 3
In Athena engine version 3, you wrap date_parse in try, as in try(date_parse(, to get string, format))NULL for a string that does not parse. The date_parse function uses different format specifiers, in which %i is minutes and %s is seconds. In try_strptime, write %M for minutes and %S for seconds.