Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Date and time

The largest single category in SQE. Two layers stack:

  1. DataFusion native functions: date_part, date_trunc, date_bin, extract, now, the to_timestamp_* family, make_date. Powerful but uses DataFusion-specific names.
  2. Trino aliases registered by sqe-trino-functions: year(), month(), day(), date_add(), date_diff(), format_datetime(). These cover every Trino date function so dbt-trino models work unmodified.

Snowflake and Spark also map well via the Trino layer.

Construction and current value

FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
now()sqe-trino-functionsReturns TIMESTAMP(6). Stable within one query. trino_functions.rs:55nowcurrent_timestampnow / current_timestampnow / current_timestamp
current_timestampdatafusion-builtinSame value as now(). SQL keyword, no parens.current_timestampcurrent_timestampcurrent_timestampcurrent_timestamp
current_datedatafusion-builtinToday’s date in session timezone.current_datecurrent_datecurrent_datecurrent_date
current_timedatafusion-builtinCurrent wall-clock time.current_timecurrent_timecurrent_timecurrent_time
localtime()sqe-trino-functionsLocal time-of-day. Returns TIME(6). trino_functions.rs:64localtime---
localtimestamp()sqe-trino-functionsLocal timestamp without offset. Returns TIMESTAMP(6). trino_functions.rs:65localtimestamp---
current_timezone()sqe-trino-functions (ext)Returns the session timezone string. trino_functions_ext.rs:41current_timezone-current_timezone-
make_date(year, month, day)datafusion-builtinConstruct a DATE.-date_from_partsmake_datemake_date

Extraction (year, month, day, …)

These return integer parts. SQE registers Trino names; DataFusion’s extract and date_part are also available for SQL standard syntax.

FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
year(d)sqe-trino-functionstrino_functions.rs:28yearyearyearyear
month(d)sqe-trino-functionstrino_functions.rs:29monthmonthmonthmonth
day(d)sqe-trino-functionstrino_functions.rs:30day / day_of_monthdaydayday
hour(d)sqe-trino-functionstrino_functions.rs:31hourhourhourhour
minute(d)sqe-trino-functionstrino_functions.rs:32minuteminuteminuteminute
second(d)sqe-trino-functionstrino_functions.rs:33secondsecondsecondsecond
millisecond(d)sqe-trino-functions (ext)Sub-second component. trino_functions_ext.rs:27millisecond---
day_of_week(d)sqe-trino-functionsISO: 1=Mon..7=Sun. trino_functions.rs:34day_of_week / dowdayofweekdayofweekdayofweek
day_of_year(d)sqe-trino-functions1..366. trino_functions.rs:35day_of_year / doydayofyeardayofyeardayofyear
quarter(d)sqe-trino-functions1..4. trino_functions.rs:36quarterquarterquarterquarter
week(d)sqe-trino-functionsISO week number. trino_functions.rs:37week / week_of_yearweekofyearweekofyearweekofyear
extract(<part> from d)datafusion-builtinSQL standard. Parts: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, DOW, DOY, EPOCH, QUARTER, WEEK.extractextractextractextract
date_part('year', d)datafusion-builtinFunction form of extract.date_partdate_part-date_part

Truncation and binning

FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
date_trunc('month', d)datafusion-builtinRound down to a calendar boundary. Parts: year, quarter, month, week, day, hour, minute, second, millisecond, microsecond.date_truncdate_truncdate_truncdate_trunc
date_bin(stride, d)datafusion-builtinBin into fixed-width buckets, e.g. INTERVAL '15' minutes. SQE-relevant for time-series rollups.-time_slice-time_bucket
last_day_of_month(d)sqe-trino-functions (ext)Last calendar day of d’s month. trino_functions_ext.rs:43last_day_of_monthlast_daylast_daylast_day

Arithmetic

FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
date_add(unit, n, d)sqe-trino-functionsunit is a string: year, quarter, month, week, day, hour, minute, second, millisecond. trino_functions.rs:40date_adddateadddate_add (different shape)date_add
date_diff(unit, d1, d2)sqe-trino-functionsDifference d2 - d1 in units. trino_functions.rs:41date_diffdatediffdatediffdate_diff
d + INTERVAL '5' DAYdatafusion-builtinSQL standard interval arithmetic.yesyesyesyes
d - INTERVAL '1' MONTHdatafusion-builtinSubtraction works the same way.yesyesyesyes

date_add example:

SELECT date_add('day', 7, DATE '2026-05-08');     -- 2026-05-15
SELECT date_add('month', -1, DATE '2026-05-08');  -- 2026-04-08
SELECT date_add('hour', 36, TIMESTAMP '2026-05-08 09:00:00');  -- 2026-05-09 21:00:00

date_diff example:

SELECT date_diff('day', DATE '2026-01-01', DATE '2026-01-06');   -- 5
SELECT date_diff('month', DATE '2026-01-01', DATE '2026-04-01'); -- 3
SELECT date_diff('year', DATE '2020-01-01', DATE '2026-01-01');  -- 6

Formatting and parsing

Two parallel formatting families:

  • Trino / MySQL %Y-%m-%d style via date_format / date_parse. Familiar to anyone who’s used strftime.
  • Java / Joda yyyy-MM-dd style via format_datetime / parse_datetime. The Trino default for new code.
FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
date_format(d, fmt)sqe-trino-functionsTrino / MySQL specifiers (%Y, %m, %d, %H, %i for minutes, %s for seconds). trino_functions.rs:51date_formatto_chardate_formatstrftime
date_parse(str, fmt)sqe-trino-functionsInverse of date_format. Returns TIMESTAMP(6). trino_functions.rs:52date_parseto_timestampto_timestampstrptime
format_datetime(d, fmt)sqe-trino-functions (ext)Java / Joda specifiers (yyyy-MM-dd HH:mm:ss). trino_functions_ext.rs:54format_datetime-date_format-
parse_datetime(str, fmt)sqe-trino-functions (ext)Inverse of format_datetime. trino_functions_ext.rs:55parse_datetime-to_timestamp-
to_iso8601(d)sqe-trino-functions (ext)RFC 3339 / ISO 8601 string. trino_functions_ext.rs:40to_iso8601---
from_iso8601_date(str)sqe-trino-functions (ext)ISO 8601 -> DATE. trino_functions_ext.rs:38from_iso8601_date---
from_iso8601_timestamp(str)sqe-trino-functions (ext)ISO 8601 -> TIMESTAMP(6). trino_functions_ext.rs:39from_iso8601_timestamp---
to_char(d, fmt)datafusion-builtinPostgres-style. Specifiers differ from Trino’s date_format.-to_chardate_formatstrftime
to_date(str [, fmt])datafusion-builtinParse to DATE.-to_dateto_datestrptime
to_timestamp(str [, fmt])datafusion-builtinParse to TIMESTAMP.parse_datetimeto_timestampto_timestampstrptime
to_timestamp_seconds(epoch)datafusion-builtinCast a unix epoch to TIMESTAMP(0).-to_timestamp-to_timestamp
to_timestamp_millis(ms)datafusion-builtinCast millis to TIMESTAMP(3).----
to_timestamp_micros(us)datafusion-builtinCast micros to TIMESTAMP(6).----
to_timestamp_nanos(ns)datafusion-builtinCast nanos to TIMESTAMP(9). V3 Iceberg ns timestamps.----

Trino-style example:

SELECT date_format(TIMESTAMP '2026-05-08 14:30:00', '%Y-%m-%d %H:%i:%s');
-- 2026-05-08 14:30:00

SELECT date_parse('2026-05-08 14:30:00', '%Y-%m-%d %H:%i:%s');
-- 2026-05-08 14:30:00 (TIMESTAMP)

Java-style example (preferred for new code):

SELECT format_datetime(TIMESTAMP '2026-05-08 14:30:00', 'yyyy-MM-dd HH:mm:ss');
-- 2026-05-08 14:30:00

Unix epoch

FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
from_unixtime(epoch)sqe-trino-functionsSeconds since epoch -> TIMESTAMP(6). trino_functions.rs:42from_unixtime-from_unixtimeto_timestamp
to_unixtime(d)sqe-trino-functionsTIMESTAMP -> seconds since epoch (Double). trino_functions.rs:43to_unixtime-unix_timestampepoch
extract(epoch from d)datafusion-builtinSQL-standard alternative for to_unixtime.yes-yesyes

Time zones

FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
with_timezone(d, tz)sqe-trino-functions (ext)Attach a timezone offset to a naive timestamp. trino_functions_ext.rs:50with_timezoneconvert_timezonefrom_utc_timestamp-
at_timezone(d, tz)sqe-trino-functions (ext)Convert a TIMESTAMP WITH TIME ZONE to another zone. trino_functions_ext.rs:51at_timezoneconvert_timezone--
timezone_hour(d)sqe-trino-functions (ext)Hour component of the offset. trino_functions_ext.rs:70timezone_hour---
timezone_minute(d)sqe-trino-functions (ext)Minute component of the offset. trino_functions_ext.rs:71timezone_minute---
expr AT TIME ZONE 'Europe/Amsterdam'datafusion-builtinSQL-standard syntax.yesyesyesyes

Misc

FunctionOriginNotesTrinoSnowflakeSpark SQLDuckDB
human_readable_seconds(n)sqe-trino-functions (ext)Format seconds as "1d 4h 30m". Useful in monitoring queries. trino_functions_ext.rs:42human_readable_seconds---

Worked example

A daily revenue rollup, time-zone aware, formatted for human reports:

SELECT
    date_format(
        with_timezone(date_trunc('day', order_ts), 'Europe/Amsterdam'),
        '%Y-%m-%d'
    ) AS day_local,
    quarter(order_ts) AS qtr,
    day_of_week(order_ts) AS dow,
    SUM(amount) AS revenue
FROM orders
WHERE order_ts >= TIMESTAMP '2026-01-01 00:00:00' AT TIME ZONE 'UTC'
GROUP BY 1, 2, 3
ORDER BY 1;

Iceberg V3 nanosecond timestamps

TIMESTAMP_NS and TIMESTAMP_NS WITH TIME ZONE only exist in Iceberg format-version 3. Adding such a column to a CREATE TABLE auto-bumps the table format version. All datetime functions operate on these the same way they operate on TIMESTAMP(6); the underlying Arrow type is Timestamp(Nanosecond, ...) instead of Timestamp(Microsecond, ...).

When you query a V3 ns column from a Trino client, Trino downscales to microseconds. SQE keeps the full precision when serving Arrow Flight SQL clients.