SQL date parsing, formatting, and time buckets

Parse date strings, format timestamps, and group events into calendar periods with Haybarn SQL date and time functions.

Choose a function

Treat parsing, grouping, and formatting as separate steps. Parse source text into a date or timestamp, group on a typed calendar bucket, then format the final label for display.

A timestamp with a time zone represents an instant; calendar grouping depends on the selected timezone. The examples here use dates or timestamps without time zones to keep the result explicit.

Group timestamps into calendar buckets

Move a date or timestamp to the start of a calendar unit, such as a month, so rows can share a grouping key.

Truncation removes the smaller time components. NULL input remains NULL.

Group timestamps into calendar buckets
SQL
Haybarn WASM 2.0.0-rc12 · In your browser

Guide example · Run to view results.

Expected result
month_startmissing
2026-11-01NULL

Watch for: Calendar buckets are not fixed durations: months have different lengths. Choose a timezone deliberately when bucketing timestamps with time zones.

Full date_trunc() reference →

All dates & time functions

61 functions in Haybarn WASM 2.0.0-rc12. Browse the filtered catalog →

  • age()

    Subtract arguments, resulting in the time difference between the two timestamps

  • current_date()
  • current_localtime()
  • current_localtimestamp()
  • date_add()
  • date_diff()

    The number of partition boundaries between the timestamps

  • date_part()

    Get subfield (equivalent to extract)

  • date_sub()

    The number of complete partitions between the timestamps

  • date_trunc()

    Move a date or timestamp to the start of a calendar unit, such as a month, so rows can share a grouping key.

  • datediff()

    The number of partition boundaries between the timestamps

  • datepart()

    Get subfield (equivalent to extract)

  • datesub()

    The number of complete partitions between the timestamps

  • datetrunc()

    Truncate to specified precision

  • day()

    Extract the day component from a date or timestamp

  • dayname()

    The (English) name of the weekday

  • dayofmonth()

    Extract the dayofmonth component from a date or timestamp

  • dayofweek()

    Extract the dayofweek component from a date or timestamp

  • dayofyear()

    Extract the dayofyear component from a date or timestamp

  • days_in_month()
  • epoch()

    Extract the epoch component from a temporal type

  • epoch_ms()

    Extract the epoch component in milliseconds from a temporal type

  • epoch_ns()

    Extract the epoch component in nanoseconds from a temporal type

  • epoch_us()

    Extract the epoch component in microseconds from a temporal type

  • get_current_time()
  • get_current_timestamp()

    Returns the current timestamp

  • hour()

    Extract the hour component from a date or timestamp

  • isoyear()

    Extract the isoyear component from a date or timestamp

  • last_day()

    Returns the last day of the month

  • make_date()

    The date for the given struct.

  • make_time()

    The time for the given parts

  • make_timestamp()

    The timestamp for the given parts

  • make_timestamp_ms()

    The timestamp for the given microseconds since the epoch

  • make_timestamp_ns()

    The timestamp for the given nanoseconds since epoch

  • make_timestamptz()
  • minute()

    Extract the minute component from a date or timestamp

  • month()

    Extract the month component from a date or timestamp

  • monthname()

    The (English) name of the month

  • pg_timezone_names()
  • quarter()

    Extract the quarter component from a date or timestamp

  • second()

    Extract the second component from a date or timestamp

  • strftime()

    Convert a date or timestamp to text with a format string, such as %Y-%m for a year and month label.

  • strptime()

    Converts the string text to timestamp applying the format strings in the list until one succeeds. Throws an error on failure. To return NULL on failure, use try_strptime.

  • time_bucket()

    Truncate TIMESTAMPTZ by the specified interval bucket_width. Buckets are aligned relative to origin TIMESTAMPTZ. The origin defaults to 2000-01-03 00:00:00+00 for buckets that do not include a month or year interval, and to 2000-01-01 00:00:00+00 for month and year buckets

  • timetz_byte_comparable()

    Converts a TIME WITH TIME ZONE to an integer sort key

  • timezone()

    Extract the timezone component from a date or timestamp

  • timezone_hour()

    Extract the timezone_hour component from a date or timestamp

  • timezone_minute()

    Extract the timezone_minute component from a date or timestamp

  • to_days()

    Construct a day interval

  • to_months()

    Construct a month interval

  • to_timestamp()

    Converts secs since epoch to a timestamp with time zone

  • to_weeks()

    Construct a week interval

  • to_years()

    Construct a year interval

  • today()
  • transaction_timestamp()

    Returns the current timestamp

  • try_strptime()

    Parse text into a timestamp with an explicit format, returning NULL when a value cannot be parsed.

  • uuid_extract_timestamp()

    Extract the timestamp for the given UUID v7.

  • week()

    Extract the week component from a date or timestamp

  • weekday()

    Extract the weekday component from a date or timestamp

  • weekofyear()

    Extract the weekofyear component from a date or timestamp

  • year()

    Extract the year component from a date or timestamp

  • yearweek()

    Extract the yearweek component from a date or timestamp