Date/Time Functions
Date/Time Functions
Functions for manipulating and extracting information from date and time values. The examples use fixed dates so their meaning is easy to check. Date parts such as day, month, and hour are unquoted keywords.
CURRENT_DATE
Returns the current date when the query begins execution. This value remains constant throughout the query.
CURRENT_DATETIME
Returns the current date and time (timestamp) when the query begins execution. This value remains constant throughout the query.
CURRENT_TIMESTAMP
Returns the current timestamp. Timezone behavior depends on the backend.
With pytrilogy 0.3.404, this expression compiles but the emitted current_timestamp() call does not execute on DuckDB. Use current_datetime() for a current date/time value on DuckDB.
DATE
Extracts the date part from a timestamp, datetime, or string, or casts a value to the DATE type. If the input is already a DATE, it is returned unchanged.
DATETIME
Casts the input value (date, timestamp, or string) to the DATETIME type.
TIMESTAMP
Casts the input value (date, datetime, or string) to the TIMESTAMP type.
DATE_TRUNC {#date-truncate}
Truncates a date/time value to an unquoted precision such as year, month, or day. date_truncate is an alias. The result type depends on the input and backend; cast to date when you need a date-only key.
DATE_PART
Extracts a subfield as an integer. Use an unquoted date part such as year, month, day, hour, or day_of_week.
DATE_ADD
Adds a specified integer interval to a date/time value. The unit of the interval is specified by the date part argument (e.g., day, month, year).
DATE_SUB
Subtracts a specified integer interval from a date/time value. The unit of the interval is specified by the date part argument.
DATE_DIFF
Calculates the difference from the first date/time argument (start) to the second (end), in the units specified by the third argument.
Unix epoch seconds {#unix-to-timestamp}
Use date_add to add epoch seconds to January 1, 1970. The current parser does not expose a callable unix_to_timestamp function.
select date_add('1970-01-01 00:00:00'::datetime, second, 3600) as event_time;
This returns 1970-01-01 01:00:00. The result is a datetime; apply the UTC convention explicitly in your model. For epoch milliseconds, convert to seconds first, taking account of any subsecond precision you need to retain.
SECOND
Extracts the second component (0-59) from a date/time value as an integer.
MINUTE
Extracts the minute component (0-59) from a date/time value as an integer.
HOUR
Extracts the hour component (0-23) from a date/time value as an integer.
DAY
Extracts the day of the month component (1-31) from a date/time value as an integer.
DAY_NAME
Extracts the name of the day of the week (e.g., 'Monday') from a date/time value.
WEEK
Extracts a week number. ISO week behavior and year boundaries depend on the backend.
MONTH
Extracts the month component (1-12) from a date/time value as an integer.
MONTH_NAME
Extracts the name of the month (e.g., 'January') from a date/time value.
QUARTER
Extracts the quarter component (1-4) from a date/time value as an integer.
YEAR
Extracts the year component from a date/time value as an integer.
DAY_OF_WEEK
Extracts the day of the week as an integer. In the DuckDB backend, Sunday is 1 and Saturday is 7; check numbering when changing backends.
DATE_SPINE
Generates a continuous series of dates between a start and end date, inclusive. This is useful for creating date dimensions.