Current date and time functions
Use these functions to get the date or time at which the current query started, in UTC, or that time minus an interval. Every call in a query returns the same value, and the value changes from one query to the next.
ago function
Returns the date and time that is interval before the time at which the current query started, as a TIMESTAMP WITH TIME ZONE in UTC. The expression ago( returns the same value as interval)now() - , so the result varies with the time at which you run the query. If intervalinterval is NULL, the result is NULL.
Use ago to keep recent rows, as in WHERE .order_ts >= ago(INTERVAL 7 DAY)
Syntax
ago(interval)
Arguments
The ago function takes the following argument:
interval-
The length of time to go back, as an
INTERVAL, or aVARCHARvalue that can be cast to an interval, such as'1 day'.
Return type
TIMESTAMP WITH TIME ZONE
Examples
The following SQL query shows that the result is earlier than the current time. The result varies from run to run, so the query compares it instead of showing it.
SELECT ago(INTERVAL 1 DAY) < now() AS is_earlier
The query returns the following result.
| is_earlier |
|---|
|
The following SQL query shows that the result is exactly the interval before the current time.
SELECT now() - ago( INTERVAL '2 hours' ) = INTERVAL '2 hours' AS is_two_hours
The query returns the following result.
| is_two_hours |
|---|
|
The following SQL query counts the rows from the last 7 days. Only the row with the current time is recent.
SELECT count(*) AS recent_rows FROM (VALUES (now()), (TIMESTAMPTZ '2000-01-01 00:00:00+00') ) AS t(ts) WHERE ts >= ago(INTERVAL 7 DAY)
The query returns the following result.
| recent_rows |
|---|
|
current_date function
Returns the date on which the current query started, in the session time zone, UTC. The value is the same for every row and every call in a query, and it varies with the date on which the query runs.
You can write current_date with or without parentheses.
Syntax
current_date
current_date()
Return type
DATE
Examples
The following SQL query shows that current_date is the UTC date of the current time. The date varies from day to day, so the query compares it instead of showing it.
SELECT current_date = CAST(now() AS DATE) AS is_today
The query returns the following result.
| is_today |
|---|
|
The following SQL query counts the rows whose date is in the last 7 days. Subtracting an integer from a DATE subtracts that many days.
SELECT count(*) AS recent_rows FROM (VALUES (current_date), (DATE '2000-01-01') ) AS t(d) WHERE d >= current_date - 7
The query returns the following result.
| recent_rows |
|---|
|
The following SQL query shows that the forms with and without parentheses return the same value.
SELECT current_date() = current_date AS same_value
The query returns the following result.
| same_value |
|---|
|
Considerations
Because the session time zone is always UTC, current_date can be a day earlier or later than the date in your own time zone.
current_localtime function
Returns the time of day at which the current query started, in the session time zone, UTC, as a TIME value without a time zone. The value is the same for every row and every call in a query, and it varies with the time at which the query runs.
localtime, written without parentheses, returns the same value.
Syntax
current_localtime()
localtime
Return type
TIME
Examples
The following SQL query shows that current_localtime is the time-of-day part of current_localtimestamp. The time varies from run to run, so the query compares it instead of showing it.
SELECT current_localtime() = CAST( current_localtimestamp() AS TIME ) AS same_time
The query returns the following result.
| same_time |
|---|
|
The following SQL query shows that localtime returns the same value.
SELECT localtime = current_localtime() AS same_value
The query returns the following result.
| same_value |
|---|
|
current_localtimestamp function
Returns the date and time at which the current query started, in the session time zone, UTC, as a TIMESTAMP value without a time zone. The value is the same for every row and every call in a query, and it varies with the time at which the query runs.
localtimestamp, written without parentheses, returns the same value. To get the same instant as a TIMESTAMP WITH TIME ZONE, use now().
Syntax
current_localtimestamp()
localtimestamp
Return type
TIMESTAMP
Examples
The following SQL query shows that current_localtimestamp is the current time in UTC without the time zone. The time varies from run to run, so the query compares it instead of showing it.
SELECT current_localtimestamp() = CAST( now() AS TIMESTAMP ) AS is_utc_now
The query returns the following result.
| is_utc_now |
|---|
|
The following SQL query shows that localtimestamp returns the same value.
SELECT localtimestamp = current_localtimestamp() AS same_value
The query returns the following result.
| same_value |
|---|
|
The following SQL query shows that the date part of the result is current_date.
SELECT CAST( current_localtimestamp() AS DATE ) = current_date AS same_day
The query returns the following result.
| same_day |
|---|
|
get_current_time function
Returns the current time of day as a TIME WITH TIME ZONE value, in the session time zone, which is UTC. The value does not change while a query runs, so every call in the same query returns the same value. The result varies from query to query.
The result equals the time part of get_current_timestamp(). The SQL keyword current_time returns the same value. To get the current time without a time zone, use the current_localtime function.
Syntax
get_current_time()
Return type
TIME WITH TIME ZONE
Examples
The following SQL query returns the data type of the result. The time itself varies with each query, so the query does not show it.
SELECT typeof(get_current_time()) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query checks that the result equals the time part of the current timestamp.
SELECT get_current_time() = CAST( get_current_timestamp() AS TIME WITH TIME ZONE ) AS is_same
The query returns the following result.
| is_same |
|---|
|
The following SQL query checks that the current_time keyword returns the same value.
SELECT current_time = get_current_time() AS is_same
The query returns the following result.
| is_same |
|---|
|
The following SQL query checks that the time is in UTC.
SELECT CAST( get_current_time() AS VARCHAR ) LIKE '%+00' AS is_utc
The query returns the following result.
| is_utc |
|---|
|
Considerations
Because the session time zone is UTC, a cast of the result to VARCHAR ends with the offset +00.
get_current_timestamp function
Returns the current date and time as a TIMESTAMP WITH TIME ZONE value with microsecond precision, in the session time zone, which is UTC. The value does not change while a query runs, so every call in the same query returns the same value. The result varies from query to query.
now and transaction_timestamp are aliases of get_current_timestamp, and the SQL keyword current_timestamp returns the same value. To get the current date and time without a time zone, use the current_localtimestamp function. To get only the date, use the current_date function.
Syntax
get_current_timestamp()
now()
transaction_timestamp()
Return type
TIMESTAMP WITH TIME ZONE
Examples
The following SQL query returns the data type of the result. The timestamp itself varies with each query, so the query does not show it.
SELECT typeof(get_current_timestamp()) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query checks that get_current_timestamp and its aliases return the same value in one query.
SELECT get_current_timestamp() = now() AND now() = transaction_timestamp() AS is_same
The query returns the following result.
| is_same |
|---|
|
The following SQL query checks that the current_timestamp keyword returns the same value as now.
SELECT current_timestamp = now() AS is_same
The query returns the following result.
| is_same |
|---|
|
The following SQL query checks that the timestamp is in UTC.
SELECT CAST(now() AS VARCHAR) LIKE '%+00' AS is_utc
The query returns the following result.
| is_utc |
|---|
|
Considerations
Because the session time zone is UTC, a cast of the result to VARCHAR ends with the offset +00.
Differences from Athena engine version 3
In Athena engine version 3, now() and current_timestamp return a timestamp(3) with time zone value, which has millisecond precision. In DuckDB, the value has microsecond precision.
In Athena engine version 3, a cast of now() to VARCHAR ends with the time zone name UTC. In DuckDB, it ends with the offset +00.
today function
Returns the current date as a DATE value, in the session time zone, which is UTC. The value does not change while a query runs, so every row of a query gets the same date. The result is the same as current_date.
The result varies with the date on which the query runs, so the examples show properties of the result instead of a date.
Syntax
today()
Return type
DATE
Examples
The following SQL query checks that today returns the same date as current_date.
SELECT today() = current_date AS is_current_date
The query returns the following result.
| is_current_date |
|---|
|
The following SQL query uses typeof to show that today returns a DATE value.
SELECT typeof(today()) AS result
The query returns the following result.
| result |
|---|
|
Considerations
today uses UTC, not your local time zone. Near midnight, the result can be one day ahead of or behind the date where you are. To get the date in another time zone, convert the current timestamp first, for example CAST(timezone('America/New_York', now()) AS DATE).