Date / Time Functions
44 functions for date arithmetic, part extraction, formatting, and timezone handling. The source column is always the implicit first input.
| Function | Syntax | Parameters | Notes |
|---|---|---|---|
trim_timestamp | trim_timestamp(column, unit) | unit default DAY (also MONTH, YEAR). | Truncates the time portion to the given unit. |
date_add | date_add(column, amount, unit) | amount default 0. unit default DAY (also WEEK, MONTH, YEAR, HOUR, MINUTE, SECOND). | |
date_sub | date_sub(column, amount, unit) | Same as date_add. | Subtracts instead of adds. |
to_timestamp | to_timestamp(column, format) | format default yyyy-MM-dd HH:mm:ss. | |
unix_timestamp | unix_timestamp(column, format) | format optional. | Returns epoch seconds. |
from_unixtime | from_unixtime(column, format) | format default yyyy-MM-dd HH:mm:ss. | Inverse of unix_timestamp. |
date_format | date_format(column, format) | format default yyyy-MM-dd. | |
extract_date_part | extract_date_part(column, part) | part default MONTH — one of YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, WEEK, ISO_WEEK, QUARTER, DAYOFWEEK, DAYOFYEAR, WEEK_OF_MONTH, DAY_OF_QUARTER, WEEK_OF_QUARTER, ISO_YEAR, MILLISECOND, MICROSECOND. | General-purpose alternative to the dedicated per-part functions below. |
date_diff | date_diff(startDate, endDate, unit) | startDate, endDate — column refs. unit — DAY / MONTH / YEAR. | |
current_date | current_date() | None. | |
current_time | current_time() | None. | |
current_timestamp | current_timestamp() | None. | Aliases: sysdate, systimestamp, localtimestamp, now. |
utc_timestamp | utc_timestamp() | None. | |
add_months | add_months(column, months) | months default 1. | |
months_between | months_between(startDate, endDate) | startDate, endDate — column refs. | |
last_day | last_day(column) | None. | Last day of the column's month. |
next_day | next_day(column, weekday) | weekday default MONDAY. | |
date_trunc | date_trunc(column, unit) | unit default DAY. | |
trunc_date | trunc_date(column, unit) | unit default DAY. | Alias family of date_trunc. |
round_date | round_date(column, unit) | unit default DAY. | Rounds instead of truncating. |
year / month / day / hour / minute / second | year(column) etc. | None. | Single date/time part extractors. |
week / iso_week | week(column) | None. | ISO week number. |
quarter | quarter(column) | None. | |
dayofweek | dayofweek(column) | None. | |
dayofyear | dayofyear(column) | None. | |
week_of_month | week_of_month(column) | None. | |
day_of_quarter | day_of_quarter(column) | None. | |
week_of_quarter | week_of_quarter(column) | None. | |
iso_year | iso_year(column) | None. | |
millisecond | millisecond(column) | None. | |
microsecond | microsecond(column) | None. | |
age | age(column, endDate) | endDate default CURRENT_DATE. | Interval between column and endDate. |
make_date / date_from_parts | make_date(year, month, day) | year default 2024, month default 1, day default 1. | Builds a date from literal/column parts. |
make_time / time_from_parts | make_time(hour, minute, second) | All default 0. | |
timestamp_from_parts | timestamp_from_parts(year, month, day, hour, minute, second) | Combines date and time part builders. | |
at_time_zone | at_time_zone(column, sourceTimezone, timezone) | sourceTimezone, timezone default UTC. | |
from_tz | from_tz(column, sourceTimezone, targetTimezone) | Both default UTC. | |
leap_year_check | leap_year_check(column) | None. | Returns boolean. |
business_days | business_days(column, endDate) | endDate default CURRENT_DATE. | Counts weekdays between column and endDate. |