Applies to VAST Cluster 5.5.
Functions › Date and time functions › Parts of a date or time. Returns the seconds of a timestamp or time of day, including their fraction, as a whole number of milliseconds from 0 to 59999. On an interval, it returns the seconds within the minute of its time part, in milliseconds.
Syntax
millisecond(value)
| Argument | Type | Description |
|---|---|---|
value |
TIMESTAMP (also TIMESTAMP_NS, TIMESTAMP_MS, TIMESTAMP_S), TIME, TIME WITH TIME ZONE, DATE or INTERVAL |
The value to take the milliseconds of. |
Returns: BIGINT.
Same as: date_part('millisecond', value) and extract(millisecond FROM value); the part also has the names milliseconds, ms and msec. See date_part.
Set up the examples
The examples connect with the ADBC driver, as in VAST Query Engine (ADBC), and work in a schema called docs_sql_fn_date_time_millisecond. Set QE_ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and VASTDB_BUCKET (SQL reference), and run the blocks in order in one Python session. Below each block is the output you should see.
This block connects, defines the run() and plan() helpers, and creates a table of five events. Each has a date (d), a timestamp (ts), a time of day (t) and a timestamp with nanoseconds (tns). Row 4 has no values, and row 5 is the last moment of 1969:
import os
import re
import adbc_driver_manager
import adbc_driver_manager.dbapi as dbapi
import adbc_driver_vastdb
BUCKET = os.environ["VASTDB_BUCKET"]
SCHEMA = f'"{BUCKET}/docs_sql_fn_date_time_millisecond"'
conn = dbapi.connect(
driver=adbc_driver_vastdb.get_driver_path(),
db_kwargs={
"vast.db.endpoint": os.environ["QE_ENDPOINT"],
"vast.db.access_key": os.environ["AWS_ACCESS_KEY_ID"],
"vast.db.secret_key": os.environ["AWS_SECRET_ACCESS_KEY"],
},
autocommit=True,
)
def server_message(e):
"""The server's message in a driver error, without the request ID or the echoed statement."""
text = re.sub(r"\[req=0x[0-9a-f]+\]: ", "", str(e))
found = (re.search(r'message: "(.*?)", request_id', text, re.S)
or re.search(r"description: (.*)\)", text, re.S))
message = found.group(1) if found else text.strip().splitlines()[-1]
message = re.sub(r"^Plan query: '.*?' failed with: ", "", message, flags=re.S)
message = re.split(r"\\n|\n", message)[0].replace('\\"', '"')
return message.replace(BUCKET, "<bucket>")
def run(sql):
"""Run one statement. Print the rows it returns, or the error."""
try:
with conn.cursor() as cur:
cur.execute(sql)
rows = cur.fetchall() if cur.description else []
except (adbc_driver_manager.Error, OSError) as e:
print(f"{type(e).__name__}: {server_message(e)}")
else:
print(rows)
def plan(sql):
"""Print the plan without its ~N Rows lines: the table's row count, which can still be 0 just after an insert."""
with conn.cursor() as cur:
cur.execute("EXPLAIN " + sql)
steps = cur.fetchall()[0][1]
print("\n".join(line for line in steps.splitlines() if "Rows" not in line))
TABLE = f"{SCHEMA}.events"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, d DATE, ts TIMESTAMP, t TIME, tns TIMESTAMP_NS)")
run(f"INSERT INTO {TABLE} VALUES "
"(1, DATE '2026-02-28', TIMESTAMP '2026-02-28 23:15:00', TIME '23:15:00', "
"CAST('2026-02-28 23:15:00.123456789' AS TIMESTAMP_NS)), "
"(2, DATE '2026-03-01', TIMESTAMP '2026-03-01 08:30:45.123456', TIME '08:30:45.123456', "
"CAST('2026-03-01 08:30:45.123456' AS TIMESTAMP_NS)), "
"(3, DATE '2024-12-30', TIMESTAMP '2024-12-30 12:00:00', TIME '12:00:00', "
"CAST('2024-12-30 12:00:00' AS TIMESTAMP_NS)), "
"(4, NULL, NULL, NULL, NULL), "
"(5, DATE '1969-12-31', TIMESTAMP '1969-12-31 23:59:59.999999', TIME '23:59:59.999999', "
"CAST('1969-12-31 23:59:59.999999999' AS TIMESTAMP_NS))")
[]
[]
[(5,)]
What it does
millisecond returns the seconds part of a timestamp or time of day including the whole seconds, in milliseconds: millisecond(TIMESTAMP '2024-02-29 13:45:30.123456') is 30123 (30 seconds and 123 milliseconds), not 123. Digits below a millisecond are dropped, not rounded: TIME '00:00:59.999999' gives 59999. The result runs from 0 to 59999.
It works on every timestamp precision. A TIMESTAMP_S value has no fraction, so millisecond(TIMESTAMP_S '2024-02-29 13:45:30') is 30000. A DATE has no time, so its millisecond is always 0.
To get only the fraction, take the remainder after dividing by 1000: millisecond(ts) % 1000.
On an INTERVAL, millisecond returns the seconds within the minute of its time part, in milliseconds:
| Interval | millisecond |
Why |
|---|---|---|
INTERVAL '1 year 2 months 3 days 04:05:06.789' |
6789 |
The seconds of 04:05:06.789 |
INTERVAL '90 seconds' |
30000 |
90 seconds is 1 minute and 30 seconds |
INTERVAL '1.5 seconds' |
1500 |
|
INTERVAL '25 months 40 days 30 hours' |
0 |
No seconds in the time part |
INTERVAL '-1 year -2 months -3 days -04:05:06.789' |
-6789 |
A negative interval gives a negative result |
NULLs and edge cases
| Input | Result |
|---|---|
| A NULL value (row 4) | NULL |
DATE 'infinity', TIMESTAMP '-infinity' |
NULL |
A DATE |
0 |
TIME '00:00:59.999999' |
59999 |
TIMESTAMP '1969-12-31 23:59:59.999999' |
59999 |
TIMESTAMP_S '2024-02-29 13:45:30' |
30000 |
TIMESTAMP_NS '2024-02-29 13:45:30.123456789' |
30123 |
A time with a time zone: TIMETZ '13:45:30.123456+02:30' |
30123 |
An untyped NULL: millisecond(NULL) |
Error: Could not choose a best candidate function for the function call "millisecond("NULL")" |
| A string | Error: Could not choose a best candidate function; cast it to TIME or TIMESTAMP first |
A number, boolean, BLOB or list |
Error: No function matches the given name and argument types, for example 'millisecond(INTEGER_LITERAL)' |
The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:
run(f"SELECT id, millisecond(ts), millisecond(t) FROM {TABLE} WHERE id = 4")
run("SELECT millisecond(DATE 'infinity'), millisecond(TIMESTAMP '-infinity'), millisecond(DATE '2026-03-01'), "
"millisecond(TIME '00:00:59.999999'), millisecond(TIMESTAMP '1969-12-31 23:59:59.999999'), "
"millisecond(TIMESTAMP_S '2024-02-29 13:45:30'), millisecond(TIMESTAMP_NS '2024-02-29 13:45:30.123456789'), "
"millisecond(TIMETZ '13:45:30.123456+02:30')")
run("SELECT millisecond(INTERVAL '1 year 2 months 3 days 04:05:06.789'), millisecond(INTERVAL '90 seconds'), "
"millisecond(INTERVAL '1.5 seconds'), millisecond(INTERVAL '25 months 40 days 30 hours'), "
"millisecond(INTERVAL '-1 year -2 months -3 days -04:05:06.789')")
run("SELECT millisecond(NULL)")
run("SELECT millisecond('13:45:30.123')")
run("SELECT millisecond(123)")
[(4, None, None)]
[(None, None, 0, 59999, 59999, 30000, 30123, 30123)]
[(6789, 30000, 1500, 0, -6789)]
InternalError: Binder Error: Could not choose a best candidate function for the function call "millisecond("NULL")". In order to select one, please add explicit type casts.
InternalError: Binder Error: Could not choose a best candidate function for the function call "millisecond(STRING_LITERAL)". In order to select one, please add explicit type casts.
InternalError: Binder Error: No function matches the given name and argument types 'millisecond(INTEGER_LITERAL)'. You might need to add explicit type casts.
Examples
The seconds of each event's timestamp in milliseconds, the fraction alone, and the same for the time of day:
run(f"SELECT id, millisecond(ts), millisecond(ts) % 1000, millisecond(t) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 0, 0, 0), (2, 45123, 123, 45123), (3, 0, 0, 0), (4, None, None, None), (5, 59999, 999, 59999)]
Keep only the events whose timestamp has a fraction of a second:
run(f"SELECT id FROM {TABLE} WHERE millisecond(ts) % 1000 <> 0 ORDER BY id LIMIT 10")
[(2,), (5,)]
How it runs in VAST
The Query Engine runs on the cluster's CNodes, and a query on a table starts with a scan in VAST DataBase (VAST_SCAN). VAST DataBase can evaluate a comparison of a column with a constant, but not a function of a column, so millisecond() is always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine computesmillisecond()for each row it returns. - In
WHERE, the scan sends the column for every row, and aFILTERstep in the Query Engine computesmillisecond()and keeps the rows that match. No rows are skipped in storage.
The plan of the WHERE example shows the FILTER step above the scan:
plan(f"SELECT id FROM {TABLE} WHERE millisecond(ts) % 1000 <> 0")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (("millisecond"(ts) % 1000│
│ ) != 0) │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ ts │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Combine it with a range on the raw column. In
ts >= TIMESTAMP '2026-03-01 00:00:00' AND ts < TIMESTAMP '2026-03-02 00:00:00' AND millisecond(ts) % 1000 <> 0, the range is still evaluated in VAST DataBase, somillisecond()runs only on the rows of that day. The plan'sFILTERstep holds only themillisecond()condition:
plan(f"SELECT id FROM {TABLE} WHERE ts >= TIMESTAMP '2026-03-01 00:00:00' AND ts < TIMESTAMP '2026-03-02 00:00:00' "
"AND millisecond(ts) % 1000 <> 0")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (("millisecond"(ts) % 1000│
│ ) != 0) │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ ts │
│ id │
│ │
└───────────────────────────┘
- Store the value if you filter on it often. Compute it when you write the rows and keep it in its own column: a comparison of that column with a constant is pushed down,
millisecond(ts) % 1000 <> 0isn't.
Limitations and errors
- No time-zone support for timestamps. On a
TIMESTAMP WITH TIME ZONEvalue, such asnow(),millisecondfails with anExtension Autoloading Error. Cast the value toTIMESTAMPfirst; see time zones. ATIME WITH TIME ZONEvalue works. - Truncated, not rounded. Digits below a millisecond are dropped. Use microsecond or nanosecond to keep them.
TIMESTAMP_NSjust before 1970. Row 5'stns,1969-12-31 23:59:59.999999999, gives0, not59999. Store such values asTIMESTAMP, or use nanosecond: row 5'snanosecond(tns)is59999999999.
run(f"SELECT id, millisecond(tns), millisecond(ts), nanosecond(tns) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 123, 0, 123456789), (2, 45123, 45123, 45123456000), (3, 0, 0, 0), (4, None, None, None), (5, 0, 59999, 59999999999)]
Similar functions
| Function | Returns | Result for TIMESTAMP '2024-02-29 13:45:30.123456' (millisecond gives 30123) |
|---|---|---|
| second | The whole second, 0 to 59 | 30 |
| microsecond | The seconds with their fraction, in microseconds | 30123456 |
| nanosecond | The seconds with their fraction, in nanoseconds | 30123456000 |
date_trunc with 'millisecond' |
The value cut to the millisecond, as a TIMESTAMP |
2024-02-29 13:45:30.123 |
strftime with '%S.%g' |
The seconds and milliseconds as text | '30.123' |
run("SELECT millisecond(TIMESTAMP '2024-02-29 13:45:30.123456'), second(TIMESTAMP '2024-02-29 13:45:30.123456'), "
"microsecond(TIMESTAMP '2024-02-29 13:45:30.123456'), nanosecond(TIMESTAMP '2024-02-29 13:45:30.123456'), "
"date_trunc('millisecond', TIMESTAMP '2024-02-29 13:45:30.123456'), "
"strftime(TIMESTAMP '2024-02-29 13:45:30.123456', '%S.%g')")
[(30123, 30, 30123456, 30123456000, datetime.datetime(2024, 2, 29, 13, 45, 30, 123000), '30.123')]
Clean up:
run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]
See also
- date_part: any part of a value, by name.
- Date and time functions: all the date and time functions.
- Where filters run and pushdowns.
- EXPLAIN: how to read a plan.