Hologres supports the following date and time functions for type conversion, arithmetic, field extraction and truncation, and retrieving the current date or time.
Function overview
Type conversion functions
| Function | Description | Example | Result |
|---|---|---|---|
MAKE_DATE | Creates a date from year, month, and day values. Default time range: 1925–2282. | MAKE_DATE(2013, 7, 15) | 2013-07-15 |
TO_CHAR | Converts a timestamp, integer, or number to a string. | TO_CHAR(current_timestamp, 'YYYY-MM-DD') | 2024-08-05 |
TO_DATE | Converts a string to a date. Default time range: 1925–2282. | TO_DATE('05 Dec 2000', 'DD Mon YYYY') | 2000-12-05 |
TO_TIMESTAMP | Converts a string or a Unix epoch time to a timestamp. Default time range: 1925–2282. | TO_TIMESTAMP(163280296) | 1975-03-06 03:38:16+08 |
Date/time arithmetic functions
| Function | Description | Example | Result |
|---|---|---|---|
ADD_MONTHS | Adds months to a date. Oracle-compatible; requires the orafce extension. | ADD_MONTHS(current_date, 2) | 2024-10-05 |
DATEADD | Adds or subtracts a time interval from a date. Default time range: 1925–2282. Available in V2.0.31–V2.1.0 and V2.1.13+. | DATEADD(a, 1, 'mm') | 2005-03-28 00:00:00 |
DATEDIFF | Returns the difference between two dates or timestamps in a specified unit. Default time range: 1925–2282. Available in V2.0.31–V2.1.0 and V2.1.13+. | DATEDIFF(a, '2005-03-02', 'mi') | -2880 |
MONTHS_BETWEEN | Returns the number of months between two dates. Oracle-compatible; requires the orafce extension. | MONTHS_BETWEEN('2022-01-01', '2021-11-01') | 2 |
NEXT_DAY | Returns the date of the first specified weekday after a given date. Oracle-compatible; requires the orafce extension. | NEXT_DAY('2022-05-01', 'FRIDAY') | 2022-05-06 |
| + | Adds date and time values. | date '2001-09-28' + integer '7' | 2001-10-05 |
| - | Subtracts date and time values. | date '2001-10-01' - date '2001-09-28' | 3 |
| * | Multiplies an interval by a number. | 21 * interval '3 day' | 0 years 0 mons 63 days 0 hours 0 mins 0.0 secs |
| / | Divides an interval by a number. | interval '1 hour' / 1.5 | 0 years 0 mons 0 days 0 hours 40 mins 0.0 secs |
Date/time field extraction and truncation
| Function | Description | Example | Result |
|---|---|---|---|
DATE_PART | Extracts a subfield from a timestamp. Equivalent to EXTRACT. | DATE_PART('hour', timestamp '2001-02-16 16:38:40') | 16 |
DATE_TRUNC | Truncates a timestamp to a specified precision. | DATE_TRUNC('day', timestamptz '2001-02-16 20:38:40+08') | 2001-02-16 00:00:00+08 |
EXTRACT | Extracts a subfield from a timestamp. Equivalent to DATE_PART. | EXTRACT(hour FROM timestamp '2001-02-16 20:38:40') | 20 |
LAST_DAY | Returns the last day of the month. Default time range: 1925–2282. Available in V2.0.31–V2.1.0 and V2.1.13+. | LAST_DAY('2004-02-28') | 2004-02-29 |
ORACLE_LAST_DAY | Returns the last day of the month. Oracle-compatible; requires the orafce extension. | ORACLE_LAST_DAY('2022-05-01') | 2022-05-31 |
ROUND | Rounds a date to the nearest time unit. Oracle-compatible; requires the orafce extension. | ROUND('2022-07-22'::date, 'Y') | 2023-01-01 |
TRUNC | Truncates a date or timestamp to a specified precision. Oracle-compatible; requires the orafce extension. | TRUNC('2022-05-22'::date, 'Y') | 2022-01-01 |
Current date/time functions
| Function | Description | Return type |
|---|---|---|
CLOCK_TIMESTAMP() | Returns the actual current time. Changes on each call, even within a single statement. | TIMESTAMPTZ |
CURRENT_DATE | Returns the current date at transaction start. | DATE |
CURRENT_TIMESTAMP | Returns the timestamp at transaction start. Equivalent to TRANSACTION_TIMESTAMP() and NOW(). | TIMESTAMPTZ |
LOCALTIMESTAMP | Returns the timestamp at transaction start, without time zone. | TIMESTAMP |
NOW() | Returns the timestamp at transaction start. Equivalent to CURRENT_TIMESTAMP and TRANSACTION_TIMESTAMP(). | TIMESTAMPTZ |
STATEMENT_TIMESTAMP() | Returns the timestamp at the start of the current statement. | TIMESTAMPTZ |
TIMEOFDAY() | Returns the actual current time as a formatted text string. | TEXT |
TRANSACTION_TIMESTAMP() | Returns the timestamp at transaction start. Equivalent to CURRENT_TIMESTAMP and NOW(). | TIMESTAMPTZ |
Other functions
| Function | Description | Example | Result |
|---|---|---|---|
ISFINITE | Tests whether a date or timestamp is finite (not infinity or -infinity). | ISFINITE(date '2001-02-16') | t |
Supported date parts and units
The following table shows the date part values accepted by DATEADD, DATEDIFF, DATE_PART, DATE_TRUNC, and EXTRACT. Not all parts apply to every function.
| Date part | Abbreviations | DATEADD | DATEDIFF | DATE_PART / EXTRACT | DATE_TRUNC |
|---|---|---|---|---|---|
year | yyyy, yy | ✓ | ✓ | ✓ | ✓ |
quarter | — | — | — | ✓ | ✓ |
month | mm, mon | ✓ | ✓ | ✓ | ✓ |
week | — | — | — | ✓ | ✓ |
day | dd | ✓ | ✓ | ✓ | ✓ |
hour | hh | ✓ | ✓ | ✓ | ✓ |
minute | mi | ✓ | ✓ | ✓ | ✓ |
second | ss | ✓ | ✓ | ✓ | ✓ |
century | — | — | — | ✓ | ✓ |
decade | — | — | — | ✓ | ✓ |
dow | — | — | — | ✓ (0=Sunday) | — |
isodow | — | — | — | ✓ (7=Sunday) | — |
doy | — | — | — | ✓ | — |
epoch | — | — | — | ✓ | — |
Extended time range support (GUC parameter)
By default, TO_CHAR, TO_DATE, and TO_TIMESTAMP support dates in the range 1925–2282. To support all time ranges, run one of the following statements before your SQL query.
Available in Hologres V1.1.31 and later:
SET hg_experimental_functions_use_pg_implementation = 'to_char';
SET hg_experimental_functions_use_pg_implementation = 'to_date';
SET hg_experimental_functions_use_pg_implementation = 'to_timestamp';
-- Or enable all three at once:
SET hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp';Setting this GUC parameter degrades query performance by approximately 50%. In Hologres V1.1.42 and later, the performance impact is reduced to about 20%.
Type conversion functions
TO_CHAR
Converts a timestamp, integer, or number to a string.
Syntax
TO_CHAR(TIMESTAMP | TIMESTAMPTZ, TEXT)
TO_CHAR(INT, TEXT)
TO_CHAR(DOUBLE PRECISION, TEXT)Return type: TEXT
Usage notes
Supports both 24-hour (
HH24) and 12-hour (HH12) formats. The default is 12-hour.Common format specifiers:
YYYY(year),MM(month),DD(day),HH(hour),MI(minute),SS(second).For timestamps with time zone (
TIMESTAMPTZ): if no time zone is specified, the value is converted to the system time zone (UTC+8 by default) before formatting. UseAT TIME ZONEto convert to a different time zone.To extend the supported time range beyond 1925–2282, see Extended time range support.
Examples
Convert a timestamp to 24-hour format:
-- Returns: 13:48:30
SELECT TO_CHAR(current_timestamp, 'HH24:MI:SS');
-- Returns: 2024-08-05
SELECT TO_CHAR(current_timestamp, 'YYYY-MM-DD');Convert a timestamp to 12-hour format:
-- Returns: 01:50:42 PM
SELECT TO_CHAR(current_timestamp, 'HH12:MI:SS AM');
-- Returns: 12:30:00 AM
SELECT TO_CHAR(time '00:30:00', 'HH12:MI:SS AM');Convert a TIMESTAMPTZ column — the value is converted to the system time zone (UTC+8) by default:
CREATE TABLE time_test(a TEXT, b TIMESTAMPTZ);
INSERT INTO time_test VALUES ('2001-09-28 03:00:00', '2004-10-19 10:23:54+08');
-- Returns: 10:23:54
SELECT TO_CHAR(b, 'HH24:MI:SS') FROM time_test;Convert a TIMESTAMPTZ column to a specific time zone using AT TIME ZONE:
CREATE TABLE timestamptz_test(a TIMESTAMPTZ);
INSERT INTO timestamptz_test VALUES ('2023-03-21 10:23:54+02');
-- Without a time zone override: converted to system time zone (UTC+8). Returns: 2023-03-21 16:23:54
SELECT TO_CHAR(a, 'YYYY-MM-DD HH24:MI:SS') FROM timestamptz_test;
-- With the US/Eastern time zone. Returns: 2023-03-21 04:23:54
SELECT TO_CHAR(a AT TIME ZONE 'US/Eastern', 'YYYY-MM-DD HH24:MI:SS') FROM timestamptz_test;Convert an integer to a string:
-- Returns: 125
SELECT TO_CHAR(125, '999');Convert a double-precision number to a string:
-- Returns: 125.8
SELECT TO_CHAR(125.8::real, '999D9');TO_DATE
Converts a string to a date. Default time range: 1925–2282.
Syntax
TO_DATE(<text_date> TEXT, <format_mask> TEXT)Parameters
| Parameter | Required | Description |
|---|---|---|
text_date | Yes | The string to convert. |
format_mask | Yes | The date format of the input string. |
Return type: DATE
Usage notes
To extend the supported time range beyond 1925–2282, see Extended time range support.
Examples
Convert a string to a date:
-- Returns: 2000-12-05
SELECT TO_DATE('05 Dec 2000', 'DD Mon YYYY');
-- Returns: 2001-03-24
SELECT TO_DATE('2001 03 24', 'YYYY-MM-DD');Convert a TEXT column to a date:
CREATE TABLE time_test(a TEXT);
INSERT INTO time_test VALUES ('2001-09-28 03:00:00');
SELECT TO_DATE(a, 'YYYY-MM-DD') FROM time_test;Result:
to_date
------------
2001-09-28TO_TIMESTAMP
Converts a string or a Unix epoch time to a TIMESTAMPTZ value. Default time range: 1925–2282.
Syntax
-- Convert a string to a timestamp
TO_TIMESTAMP(<text_date> TEXT, <format_mask> TEXT)
-- Convert a Unix epoch time (seconds since 1970-01-01 00:00:00 UTC) to a timestamp
TO_TIMESTAMP(DOUBLE PRECISION)Return type: TIMESTAMPTZ
Usage notes
The result includes the time zone offset (for example,
+08).To extend the supported time range beyond 1925–2282, see Extended time range support.
Examples
Convert a string to a timestamp:
SELECT TO_TIMESTAMP('05 Dec 2000', 'DD Mon YYYY');Result:
to_timestamp
------------------------
2000-12-05 00:00:00+08Convert a TEXT column to a timestamp:
CREATE TABLE time_test(a TEXT);
INSERT INTO time_test VALUES ('2001-09-28 03:00:00');
SELECT TO_TIMESTAMP(a, 'YYYY-MM-DD') FROM time_test;Result:
to_timestamp
------------------------
2001-09-28 00:00:00+08Convert a Unix epoch time in seconds:
-- Returns: 1975-03-06 03:38:16+08
SELECT TO_TIMESTAMP(163280296);Convert a Unix epoch time in milliseconds (divide by 1000 first):
-- Returns: 2021-09-28 12:22:41+08
SELECT TO_TIMESTAMP(1632802961000 / 1000);MAKE_DATE
Creates a date from integer year, month, and day values. Default time range: 1925–2282.
Syntax
MAKE_DATE(<year> INT, <month> INT, <day> INT)Return type: DATE
Usage notes
Available in Hologres V2.0.29 and later. In write operations, arguments cannot all be constants.
Example
-- Returns: 2013-07-15
SELECT MAKE_DATE(2013, 7, 15);Result:
make_date
------------
2013-07-15Date/time arithmetic functions
DATEADD
Adds or subtracts a time interval from a date. Default time range: 1925–2282.
Syntax
DATEADD(<d> DATE | TIMESTAMP | TIMESTAMPTZ, <num> BIGINT, <str> TEXT)Parameters
| Parameter | Description |
|---|---|
d | The base date or time value. |
num | The number of units to add (positive) or subtract (negative). |
str | The time unit. See Supported date parts and units for valid values. |
Return type: Same as the input type (DATE, TIMESTAMP, or TIMESTAMPTZ).
Usage notes
Available in Hologres V2.0.31–V2.1.0 and V2.1.13 and later.
In write operations, arguments cannot all be constants.
Example
Add one month to a timestamp column:
CREATE TABLE test_dateadd (a TIMESTAMP);
INSERT INTO test_dateadd VALUES ('2005-02-28 00:00:00');
SELECT DATEADD(a, 1, 'mm') FROM test_dateadd;Result:
dateadd
---------------------
2005-03-28 00:00:00ADD_MONTHS
Adds a specified number of months to a date. This is an Oracle-compatible function that requires the orafce extension. For more information, see Supported Oracle functions.
Syntax
ADD_MONTHS(<d> DATE, <month> INT)Parameters
| Parameter | Description |
|---|---|
d | The base date. |
month | The number of months to add. |
Return type: DATE
Example
SELECT ADD_MONTHS(current_date, 2);Result:
add_months
------------
2024-10-05DATEDIFF
Returns the difference between two dates or timestamps in a specified unit. Default time range: 1925–2282.
Syntax
DATEDIFF(<d1> DATE | TIMESTAMP | TIMESTAMPTZ, <d2> DATE | TIMESTAMP | TIMESTAMPTZ, <str> TEXT)Parameters
| Parameter | Description |
|---|---|
d1 | The first date or timestamp. |
d2 | The second date or timestamp. |
str | The time unit for calculating the difference. See Supported date parts and units for valid values. |
Return type: BIGINT
Usage notes
Available in Hologres V2.0.31–V2.1.0 and V2.1.13 and later.
In write operations, arguments cannot all be constants.
The function uses full-unit truncation, not rounding. If the difference is less than one complete unit, it returns
0. For example, the year difference between2023-12-31and2024-01-01is0.To count boundary crossings instead (returning
1in the preceding example), run the following statement before your query:SET hg_experimental_datediff_use_presto_impl = off;
Example
Calculate the difference in minutes between two timestamps:
CREATE TABLE test_datediff (a TIMESTAMP);
INSERT INTO test_datediff VALUES ('2005-02-28 00:00:00');
SELECT DATEDIFF(a, '2005-03-02 00:00:00', 'mi') FROM test_datediff;Result:
datediff
----------
-2880MONTHS_BETWEEN
Returns the number of months between two dates. This is an Oracle-compatible function that requires the orafce extension. For more information, see Supported Oracle functions.
Syntax
MONTHS_BETWEEN(DATE, DATE)Return type: INT
Examples
-- Returns: 2
SELECT MONTHS_BETWEEN('2022-01-01', '2021-11-01');
-- Returns: -2
SELECT MONTHS_BETWEEN('2021-11-01', '2022-01-01');NEXT_DAY
Returns the date of the first specified weekday that follows a given date. This is an Oracle-compatible function that requires the orafce extension. For more information, see Supported Oracle functions.
Syntax
NEXT_DAY(<d> DATE, <str> TEXT | INT)Parameters
| Parameter | Description |
|---|---|
d | The starting date. |
str | The target weekday, as a string (such as Friday) or an integer from 1 to 7, where 1 = Sunday and 2 = Monday. |
Return type: DATE
Examples
-- Returns: 2022-05-06
SELECT NEXT_DAY('2022-05-01', 'FRIDAY');
-- Returns: 2022-05-06
SELECT NEXT_DAY('2022-05-01', 5);Arithmetic operators
The following operators work with date and time values.
| Operator | Left operand | Right operand | Description | Example | Result |
|---|---|---|---|---|---|
+ | DATE | INTEGER | Adds days to a date. | date '2001-09-28' + integer '7' | 2001-10-05 |
+ | DATE | TIME | Combines a date and a time to produce a timestamp. | date '2001-09-28' + time '03:00' | 2001-09-28 03:00:00 |
+ | DATE | INTERVAL | Adds an interval to a date. | date '2001-09-28' + interval '1 hour' | 2001-09-28 01:00:00 |
+ | TIMESTAMPTZ | INTERVAL | Adds an interval to a timestamp. | now() + interval '1 day' | 2022-12-08 20:09:19+08 |
- | DATE | DATE | Returns the number of days between two dates as an integer. | date '2001-10-01' - date '2001-09-28' | 3 |
- | DATE | INTEGER | Subtracts days from a date. | date '2001-10-01' - integer '7' | 2001-09-24 |
- | DATE | TIME | Subtracts a time from a date, producing a timestamp. | date '2001-09-28' - time '03:00' | 2001-09-27 21:00:00 |
- | DATE | INTERVAL | Subtracts an interval from a date. | date '2001-09-28' - interval '1 hour' | 2001-09-27 23:00:00 |
- | TIMESTAMPTZ | INTERVAL | Subtracts an interval from a timestamp. | now() - interval '2 day' | 2022-12-06 20:27:21+08 |
* | INTEGER | INTERVAL | Multiplies an interval. | 21 * interval '3 day' | 0 years 0 mons 63 days 0 hours 0 mins 0.0 secs |
/ | INTERVAL | DOUBLE PRECISION | Divides an interval. | interval '1 hour' / double precision '1.5' | 0 years 0 mons 0 days 0 hours 40 mins 0.0 secs |
Date/time field extraction and truncation
EXTRACT
Extracts a subfield (such as year, month, or day) from a timestamp expression. EXTRACT is equivalent to DATE_PART.
Syntax
EXTRACT(field FROM TIMESTAMP)Return type: DOUBLE PRECISION
Usage notes
Valid values for field: century, day, decade, dow (day of week, 0=Sunday), isodow (day of week, 7=Sunday), doy (day of year), epoch, hour, minute, month, quarter, second, week, year.
Examples
Extract the hour from a timestamp:
-- Returns: 20
SELECT EXTRACT(hour FROM timestamp '2001-02-16 20:38:40');Extract the minute from the current time:
-- Returns: 12
SELECT EXTRACT(minute FROM NOW());Get the Unix epoch time (seconds since 1970-01-01 00:00:00 UTC):
CREATE TABLE time_test(a TEXT);
INSERT INTO time_test VALUES ('2001-09-28 03:00:00');
SELECT EXTRACT(epoch FROM to_timestamp(a, 'YYYY-MM-DD')) FROM time_test;Result:
date_part
------------
1001606400Performance-optimized extract functions
Starting from Hologres V4.0, the following functions are supported for better compatibility with ClickHouse and Doris. They have the same semantics as EXTRACT(<field> FROM timestamp) but offer better performance. These functions do not support constant-only arguments.
| Function | Aliases | Equivalent to |
|---|---|---|
extract_century(timestamp) | — | extract(century from timestamp) |
extract_day(timestamp) | toDayOfMonth, day | extract(day from timestamp) |
extract_decade(timestamp) | — | extract(decade from timestamp) |
extract_dow(timestamp) | — | extract(dow from timestamp) |
extract_doy(timestamp) | toDayOfYear, dayofyear | extract(doy from timestamp) |
extract_hour(timestamp) | toHour, hour | extract(hour from timestamp) |
extract_isodow(timestamp) | dayofweek_iso | extract(isodow from timestamp) |
extract_isoweek(timestamp) | toIsoWeek, week_iso | extract(week from timestamp) |
extract_minute(timestamp) | toMinute, minute | extract(minute from timestamp) |
extract_month(timestamp) | toMonth, month | extract(month from timestamp) |
extract_quarter(timestamp) | toQuarter, quarter | extract(quarter from timestamp) |
extract_second(timestamp) | toSecond, second | extract(second from timestamp) |
extract_year(timestamp) | toYear, year | extract(year from timestamp) |
DATE_PART
Extracts a subfield from a timestamp. DATE_PART is equivalent to EXTRACT.
Syntax
DATE_PART(<str> TEXT, <d> TIMESTAMP)Parameters
| Parameter | Description |
|---|---|
str | The subfield to extract. Valid values: century, day, decade, dow, isodow, doy, epoch, hour, minute, month, quarter, second, week, year. |
d | The date/time expression. |
Return type: DOUBLE PRECISION
Examples
Get the hour from a timestamp:
SELECT DATE_PART('hour', timestamp '2001-02-16 16:38:40');Result:
date_part
-----------
16Get the ISO week number for a date:
SELECT DATE_PART('week', TO_DATE('2022-10-11', 'YYYY-MM-DD'));Result:
date_part
-----------
41Get the month number for a date:
SELECT DATE_PART('month', TO_DATE('2022-10-11', 'YYYY-MM-DD'));Result:
date_part
-----------
10DATE_TRUNC
Truncates a date or time value to a specified precision.
Syntax
DATE_TRUNC(<str> TEXT, <d> TIME | TIMESTAMP | TIMESTAMPTZ)Parameters
| Parameter | Description |
|---|---|
str | The precision to truncate to. Valid values: century, decade, year, quarter, month, week, day, hour, minute, second. |
d | The date or time value to truncate. |
Return type: TIMESTAMP or TIMESTAMPTZ (matches the input type).
Examples
Truncate a time to the hour:
SELECT DATE_TRUNC('hour', time '12:38:40');Result:
date_trunc
------------
12:00:00Truncate a timestamp to the day:
SELECT DATE_TRUNC('day', timestamptz '2001-02-16 20:38:40+08');Result:
date_trunc
------------------------
2001-02-16 00:00:00+08Truncate a timestamp to the month:
SELECT DATE_TRUNC('month', timestamp '2001-02-16 18:38:40');Result:
date_trunc
---------------------
2001-02-01 00:00:00Get the first day of the current month at 12:00:
SELECT DATE_TRUNC('month', now()) + interval '12h';Result:
?column?
---------------------
2024-08-01 12:00:00+08Get 09:00 on the current day:
SELECT DATE_TRUNC('day', now()) + interval '9h';Result:
?column?
------------------------
2024-08-08 09:00:00+08Get the start of the same weekday in the following week:
SELECT DATE_TRUNC('day', now()) + interval '7d';Result:
?column?
------------------------
2024-08-15 00:00:00+08LAST_DAY
Returns the last day of the month for a specified date. Default time range: 1925–2282.
Syntax
LAST_DAY(DATE | TIMESTAMP | TIMESTAMPTZ)Return type: DATE
Usage notes
Available in Hologres V2.0.31–V2.1.0 and V2.1.13 and later.
In write operations, arguments cannot all be constants.
Example
CREATE TABLE test_last_day (a TIMESTAMP);
INSERT INTO test_last_day VALUES ('2004-02-28 00:00:00');
SELECT LAST_DAY(a) FROM test_last_day;Result:
last_day
------------
2004-02-29ORACLE_LAST_DAY
Returns the last day of the month for a specified date. This is an Oracle-compatible function that requires the orafce extension. Default time range: 1925–2282. For more information, see Supported Oracle functions.
Syntax
ORACLE_LAST_DAY(DATE)Return type: DATE
Example
SELECT ORACLE_LAST_DAY('2022-05-01');Result:
oracle_last_day
-----------------
2022-05-31TRUNC
Truncates a date or timestamp to a specified precision. This is an Oracle-compatible function that requires the orafce extension. For more information, see Supported Oracle functions.
Syntax
TRUNC(<d> DATE | TIMESTAMP [, <str> TEXT])Parameters
| Parameter | Required | Description |
|---|---|---|
d | Yes | The date or time value to truncate. If a TIMESTAMPTZ value is passed, the time part is set to 00:00:00. |
str | No | The precision to truncate to. Defaults to day if omitted. Y returns the first day of the year; Q returns the first day of the quarter. For all supported values, see the Oracle documentation. |
Return type: DATE or TIMESTAMP
Examples
Truncate a date to the start of the year:
SELECT TRUNC('2022-05-22'::date, 'Y');Result:
trunc
------------
2022-01-01Truncate a timestamp to the start of the year:
SELECT TRUNC('2022-05-22 13:11:22'::timestamp, 'Y');Result:
trunc
---------------------
2022-01-01 00:00:00Truncate a timestamp to the start of the quarter:
SELECT TRUNC('2022-05-22 13:11:22'::timestamp, 'Q');Result:
trunc
---------------------
2022-04-01 00:00:00Truncate a timestamp to the start of the day (default):
SELECT TRUNC('2022-05-22 13:11:22'::timestamp);Result:
trunc
---------------------
2022-05-22 00:00:00ROUND
Rounds a date or timestamp to the nearest time unit. This is an Oracle-compatible function that requires the orafce extension. For more information, see Supported Oracle functions.
Syntax
ROUND(<d> DATE | TIMESTAMPTZ [, <str> TEXT])Parameters
| Parameter | Required | Description |
|---|---|---|
d | Yes | The date or time value to round. |
str | No | The time unit to round to. Defaults to day if omitted. Y rounds to the first day of the nearest year. For all supported values, see the Oracle documentation. |
Return type: DATE or TIMESTAMP
Examples
Round a date to the nearest year (before July 1 — rounds down):
SELECT ROUND('2022-05-22'::date, 'Y');Result:
round
------------
2022-01-01Round a date to the nearest year (after July 1 — rounds up):
SELECT ROUND('2022-07-22'::date, 'Y');Result:
round
------------
2023-01-01Round a timestamp to the nearest year:
SELECT ROUND('2022-07-22 13:11:22'::timestamp, 'Y');Result:
round
---------------------
2023-01-01 00:00:00Round a timestamp to the nearest day (default):
SELECT ROUND('2022-02-22 13:11:22'::timestamp);Result:
round
---------------------
2022-02-23 00:00:00Current date/time functions
CURRENT_DATE
Returns the current date at the start of the transaction.
Syntax
CURRENT_DATEReturn type: DATE
Example
SELECT CURRENT_DATE;Result:
current_date
--------------
2024-08-08CURRENT_TIMESTAMP
Returns the timestamp at the start of the current transaction. Equivalent to TRANSACTION_TIMESTAMP() and NOW().
Syntax
CURRENT_TIMESTAMPThe returned value does not change within a single transaction.
Return type: TIMESTAMPTZ
Example
SELECT CURRENT_TIMESTAMP;Result:
current_timestamp
-------------------------------
2024-08-08 14:55:11.006068+08CLOCK_TIMESTAMP
Returns the actual current time.
Syntax
clock_timestamp()UnlikeCURRENT_TIMESTAMPandNOW(), the returned value changes on each call, even within a single SQL statement.
Return type: TIMESTAMPTZ
Example
SELECT clock_timestamp();Result:
clock_timestamp
-------------------------------
2024-08-08 14:57:43.569109+08LOCALTIMESTAMP
Returns the timestamp at the start of the current transaction, without time zone information.
Syntax
LOCALTIMESTAMPReturn type: TIMESTAMP
Example
SELECT LOCALTIMESTAMP;Result:
localtimestamp
---------------------------
2024-08-08 15:00:59.13245NOW
Returns the timestamp at the start of the current transaction. Equivalent to CURRENT_TIMESTAMP and TRANSACTION_TIMESTAMP().
Syntax
NOW()The returned value does not change within a single transaction.
Return type: TIMESTAMPTZ
Example
SELECT NOW();Result:
now
-------------------------------
2024-08-08 15:02:50.270501+08STATEMENT_TIMESTAMP
Returns the timestamp at the start of the current statement.
Syntax
STATEMENT_TIMESTAMP()The value is consistent within a single statement but differs across statements within the same transaction.
Return type: TIMESTAMPTZ
Example
SELECT STATEMENT_TIMESTAMP();Result:
statement_timestamp
-------------------------------
2024-08-08 15:06:14.772939+08TIMEOFDAY
Returns the actual current time as a formatted text string. Similar to CLOCK_TIMESTAMP(), but returns a TEXT value instead of TIMESTAMPTZ.
Syntax
TIMEOFDAY()Return type: TEXT
Example
SELECT TIMEOFDAY();Result:
timeofday
-------------------------------------
Thu Aug 08 15:08:16.599369 2024 CSTTRANSACTION_TIMESTAMP
Returns the timestamp at the start of the current transaction. Equivalent to CURRENT_TIMESTAMP and NOW().
Syntax
TRANSACTION_TIMESTAMP()The returned value does not change within a single transaction.
Return type: TIMESTAMPTZ
Example
SELECT TRANSACTION_TIMESTAMP();Result:
transaction_timestamp
-------------------------------
2024-08-08 15:11:10.329005+08Other functions
ISFINITE
Tests whether a date or timestamp is finite (not infinity or -infinity).
Syntax
ISFINITE(DATE)
ISFINITE(TIMESTAMP)Return type: BOOLEAN — returns t if finite, f if infinite.
Examples
SELECT ISFINITE(date '2001-02-16');Result:
isfinite
----------
tSELECT ISFINITE(timestamp '2001-02-16 21:28:30');Result:
isfinite
----------
tExamples
The following examples show common date and time operations in Hologres.
Add 2 hours to the current timestamp:
SELECT NOW() + interval '2 hour';Result:
?column?
-------------------------------
2022-12-29 13:43:58.321104+08Get the Unix epoch time for the current timestamp:
SELECT EXTRACT(epoch FROM current_timestamp);Result:
date_part
---------------------
1672285506.296279Add a DATE value and an integer column representing months:
CREATE TABLE date_test1(a DATE, b INT);
INSERT INTO date_test1 VALUES ('2021-09-28', '12');
SELECT a + (b || ' month')::interval FROM date_test1;Result:
?column?
--------------------
2022-09-28 00:00:00Convert a numeric string to a timestamp using TO_CHAR and TO_TIMESTAMP:
SELECT TO_TIMESTAMP(TO_CHAR(20211027172045, '9999-99-99 99:99:99'), 'YYYY-MM-DD HH24:MI:SS');Result:
to_timestamp
----------------------
2021-10-27 17:20:45+08Extract the month from the current timestamp:
SELECT EXTRACT(mon FROM now());Result:
date_part
-----------
12Divide two integers with decimal precision (cast to float first):
-- Integer division discards the remainder: 10/3 = 3
-- Cast to float to get a decimal result
SELECT 10 / 3::float;Result:
?column?
---------
3.3333333333333335