This topic describes the date and time functions in Hologres and provides usage examples.
Type | Function | Description |
Type conversion functions | Creates a date from year, month, and day values. The supported time range is 1925 to 2282 by default. | |
Converts a timestamp, integer, real number, or double-precision number to a string. | ||
Converts a string to a date. The supported time range is 1925 to 2282 by default. | ||
Converts a string to a timestamp or a Unix epoch time to a timestamp. | ||
Date/time arithmetic functions | Adds a specified number of months to a date. This Oracle-compatible function requires the | |
Adds or subtracts a specified time interval from a date. The supported time range is 1925 to 2282 by default. | ||
Calculates the time difference between two dates or timestamps. The supported time range is 1925 to 2282 by default. | ||
Calculates the number of months between two dates. This Oracle-compatible function requires the | ||
Returns the date of the first specified day of the week that follows a given date. This Oracle-compatible function requires the | ||
Adds date and time values. | ||
Subtracts date and time values. | ||
Multiplies an interval by a number. | ||
Divides an interval by a number. | ||
Date/time field extraction and truncation | Extracts a subfield (such as year, month, or day) from a timestamp expression. This function is equivalent to EXTRACT. | |
Truncates a date or time value to a specified precision. | ||
Extracts a subfield (such as year, month, or day) from a timestamp expression. | ||
Returns the last day of the month for a specified date. The supported time range is 1925 to 2282 by default. | ||
Returns the last day of the month for a specified date. This Oracle-compatible function requires the | ||
Rounds a date or time value to the nearest time unit. This Oracle-compatible function requires the | ||
Truncates a date or timestamp to a specified precision. This Oracle-compatible function requires the | ||
Current date/ time functions | Returns the current time. | |
Returns the current date. | ||
Returns the start time of the current transaction. This function is equivalent to TRANSACTION_TIMESTAMP and NOW. | ||
Returns the start time of the current transaction, without time zone information. | ||
Returns the start time of the current transaction. This function is equivalent to TRANSACTION_TIMESTAMP and CURRENT_TIMESTAMP. | ||
Returns the start time of the current statement. | ||
Returns the actual current time as a formatted text string. | ||
Returns the start time of the current transaction. This function is equivalent to CURRENT_TIMESTAMP and NOW. | ||
Other functions | Tests if a date or timestamp is finite (i.e., not |
Date and time conversion functions
TO_CHAR
Description:
Converts a timestamp to a string. The supported time range is 1925 to 2282 by default.
TO_CHAR(TIMESTAMP|TIMESTAMPTZ, TEXT)Usage notes:
This function supports both 24-hour and 12-hour formats.
HH24specifies the 24-hour format, andHH12specifies the 12-hour format. The default is the 12-hour format.Common format specifiers include:
YYYYfor year,MMfor month,DDfor day,HHfor hour,MIfor minute, andSSfor second.In Hologres V1.1.31 and later, execute one of the following statements before your SQL query to support all time ranges:
set hg_experimental_functions_use_pg_implementation = 'to_char';orset hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp';.NoteUsing this GUC parameter reduces query performance by approximately 50%. In Hologres V1.1.42 and later, this performance impact is reduced to about 20%.
Converts an integer to a string.
TO_CHAR(INT, TEXT)Converts a real or double-precision number to a string.
TO_CHAR(DOUBLE PRECISION, TEXT)
Return value:
Returns a
TEXTvalue.Examples:
Convert a timestamp to 24-hour format.
-- Returned result: 13:48:30. SELECT TO_CHAR(current_timestamp, 'HH24:MI:SS'); -- Returned result: 2024-08-05. SELECT TO_CHAR(current_timestamp, 'YYYY-MM-DD');Convert a timestamp to 12-hour format.
-- Returned result: 01:50:42 PM. SELECT TO_CHAR(current_timestamp, 'HH12:MI:SS AM'); -- Returned result: 12:30:00 AM. SELECT TO_CHAR(time '00:30:00', 'HH12:MI:SS AM');Convert a timestamp field.
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'); -- Returned result: 10:23:54. SELECT TO_CHAR(b, 'HH24:MI:SS') FROM time_test; -- Convert a TEXT into a date. Returned result: 2001-09-28. SELECT TO_CHAR(to_timestamp(a, 'YYYY-MM-DD'),'YYYY-MM-DD')FROM time_test;Convert a timestamp across time zones.
CREATE TABLE timestamptz_test( a TIMESTAMPTZ); INSERT INTO timestamptz_test VALUES ('2023-03-21 10:23:54+02');If no time zone is specified, the
acolumn is converted to the system time zone (UTC+8 by default) and then formatted as a string.-- Returned result: 2023-03-21 16:23:54. SELECT TO_CHAR(a, 'YYYY-MM-DD HH24:MI:SS') FROM timestamptz_test;Specify the
US/Easterntime zone, then convert theacolumn to a string.-- Returned result: 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.
-- Returned result: 125. SELECT TO_CHAR(125, '999');Convert a double-precision number to a string.
-- Returned result: 125.8. SELECT TO_CHAR(125.8::real, '999D9');
TO_DATE
Description: Converts a string into a date. By default, only strings that describe dates in a year from 1925 to 2282 can be converted.
TO_DATE(<text_date> TEXT, <format_mask> TEXT)Usage notes:
Starting from Hologres V1.1.31, you can execute
set hg_experimental_functions_use_pg_implementation = 'to_date';orset hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp';before the SQL statement to support all time ranges.NoteIf you execute one of the preceding statements to configure the GUC parameter, the query performance degrades by about 50%. In Hologres V1.1.42 and later, the query performance degrades by about 20%.
Parameters:
text_date: required. The string that you want to convert.
format_mask: required. The date format.
Return value:
A value of the TEXT type is returned.
Examples:
Convert a string into a date.
-- Returned result: 2000-12-05. SELECT TO_DATE('05 Dec 2000', 'DD Mon YYYY'); -- Returned result: 2001-03-24. SELECT TO_DATE('2001 03 24', 'YYYY-MM-DD');Convert the value of a field of the TEXT data type into 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;The following result is returned:
to_date ------------ 2001-09-28
TO_TIMESTAMP
Description:
Converts a string to a timestamp. The supported time range is 1925 to 2282 by default.
TO_TIMESTAMP(<text_date> TEXT, <format_mask> TEXT)Usage notes:
The result includes the time zone offset (for example,
+08).In Hologres V1.1.31 and later, execute one of the following statements before your SQL query to support all time ranges:
set hg_experimental_functions_use_pg_implementation = 'to_timestamp';orset hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp';.NoteUsing this GUC parameter reduces query performance by approximately 50%. In Hologres V1.1.42 and later, this performance impact is reduced to about 20%.
Parameters:
text_date: The string to be converted to a timestamp.
format_mask: The format mask for parsing the input string.
Converts a Unix epoch time (seconds since
1970-01-01 00:00:00 UTC) to a timestamp.TO_TIMESTAMP(DOUBLE PRECISION)
Return value:
Returns a
TIMESTAMPTZvalue.Examples:
Convert a string to a timestamp.
SELECT TO_TIMESTAMP('05 Dec 2000', 'DD Mon YYYY');The following result is returned:
to_timestamp ------------------------ 2000-12-05 00:00:00+08Convert a
TEXTfield 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;The following result is returned:
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.
-- Returns: 2021-09-28 12:22:41+08. SELECT TO_TIMESTAMP(1632802961000/1000);
MAKE_DATE
Description: Creates a date from year, month, and day values. The supported time range is 1925 to 2282 by default.
MAKE_DATE(<year> INT, <month> INT, <day> INT)Usage notes:
This function is available in Hologres V2.0.29 and later. In write operations, its arguments cannot all be constants.
Return value:
Returns a
DATEvalue.Example:
-- Returns: 2013-07-15. SELECT MAKE_DATE(2013, 7, 15);The following result is returned:
make_date ------------ 2013-07-15
Date/time arithmetic functions
DATEADD
Description: Adds or subtracts a specified time interval from a date. The supported time range is 1925 to 2282 by default.
DATEADD(<d> DATE|TIMESTAMP|TIMESTAMPTZ, <num> BIGINT, <str> TEXT)Usage notes:
This function is supported in the following Hologres versions:
V2.0.31 to V2.1.0
V2.1.13 and later
NoteThis function does not support constant-only arguments in write operations.
Parameters:
d: The base date or time value.
num: The number of units to add or subtract.
str: The time unit. Valid values include
yyyyandyearfor year;mm,month, andmonfor month;
Return value:
Returns a value of the same type as the input date/time (
DATE,TIMESTAMP, orTIMESTAMPTZ).Example:
CREATE TABLE test_dateadd (a TIMESTAMP); INSERT INTO test_dateadd VALUES ('2005-02-28 00:00:00'); -- Add one month to the specified date and time value. SELECT DATEADD(a , 1, 'mm') FROM test_dateadd;The following result is returned:
dateadd --------------------- 2005-03-28 00:00:00
ADD_MONTHS
Description: Adds a specified number of months to a date. This Oracle-compatible function requires the
orafceextension. For more information, see Supported Oracle functions.ADD_MONTHS(<d> DATE, <month> INT)Parameters:
d: The base date.
month: An integer representing the number of months to add.
Return value:
Returns a
DATEvalue.Example:
SELECT ADD_MONTHS(current_date, 2);The following result is returned:
add_months ------------ 2024-10-05
DATEDIFF
Description: Calculates the time difference between two dates or timestamps based on a specified unit. The supported time range is 1925 to 2282 by default.
DATEDIFF(<d1> DATE|TIMESTAMP|TIMESTAMPTZ, <d2> DATE|TIMESTAMP|TIMESTAMPTZ, <str> TEXT)Usage notes:
This function is supported in the following Hologres versions:
V2.0.31 to V2.1.0
V2.1.13 and later
NoteThis function does not support cases where all parameters are constants.
Parameters:
d1: The first date or timestamp value.
d2: The second date or timestamp value.
str: The time unit for calculating the difference. Valid values include
yyyyandyearfor year;mm,month, andmonfor month;ddanddayfor day;hhandhourfor hour;mifor minute; andssfor second.
Return value:
Returns a
BIGINTvalue. By default, if the time difference is less than one complete unit, the function returns 0.For example, the difference between
2023-12-31and2024-01-01in years is 0.NoteTo change this behavior and count the boundary crossing (returning 1 in the preceding scenario, run
set hg_experimental_datediff_use_presto_impl = off;before your SQL query to disable this GUC parameter.Example:
CREATE TABLE test_datediff (a TIMESTAMP); INSERT INTO test_datediff VALUES ('2005-02-28 00:00:00'); -- Calculate the difference in minutes between two dates. SELECT DATEDIFF(a , '2005-03-02 00:00:00', 'mi') FROM test_datediff;The following result is returned:
datediff ---------- -2880
MONTHS_BETWEEN
Description: Calculates the number of months between two dates. This Oracle-compatible function requires the
orafceextension. For more information, see Supported Oracle functions.MONTHS_BETWEEN(DATE, DATE)Return value:
Returns an
INTvalue.Examples:
Example 1:
-- Returns result: 2. SELECT MONTHS_BETWEEN('2022-01-01', '2021-11-01');Example 2:
-- Returns result: -2. SELECT MONTHS_BETWEEN('2021-11-01', '2022-01-01');
NEXT_DAY
Description: Returns the date of the first specified day of the week that follows a given date. This Oracle-compatible function requires the
orafceextension. For more information, see Supported Oracle functions.NEXT_DAY(<d> DATE, <str> TEXT|INT)Parameters:
d: The starting date.
str: A string representing the day of the week (for example,
Friday), or an integer from 1 to 7, where 1 represents Sunday and 2 represents Monday.
Return value:
Returns a
DATEvalue.Examples:
Example 1:
-- Returned result: 2022-05-06. SELECT NEXT_DAY('2022-05-01', 'FRIDAY');Example 2:
-- Returned result: 2022-05-06. SELECT NEXT_DAY('2022-05-01', 5);
Add date/time: +
Return value type | Example | Result |
DATE | Add 7 days to a specified date. | 2001-10-05 |
Add 3 days to the current date. | 2022-12-10 | |
Add 1 day to the current time. | 2022-12-09 | |
TIMESTAMP | Add 3 hours to a specified date. The time starts from 00:00:00. | 2001-09-28 03:00:00 |
Add 1 hour to a specified date. The time starts from 00:00:00. | 2001-09-28 01:00:00 | |
TIMESTAMPTZ | Add 1 day to the current time. | 2022-12-08 20:09:19.388465+08 |
Add 1 month to the current time. | 2023-01-08 20:21:50.993481+08 | |
Add 1 years to the current time. | 2024-12-08 20:22:49.416343+08 |
Subtract date/time: -
Return value type | Example | Result |
INTEGER | Subtract two dates. | 3 |
DATE | Subtract 7 days from a specified date. | 2001-09-24 |
TIMESTAMP | Subtract 3 hours from a specified date. | 2001-09-27 21:00:00 |
Subtract 1 hour from a specified date. | 2001-09-27 23:00:00 | |
Subtract 2 days from the current time. | 2022-12-06 20:27:21.094258+08 |
Multiply interval: *
Return value type | Example | Result |
INTERVAL | Multiply an interval. |
|
Divide interval: /
Return value type | Example | Returned result |
INTERVAL | Divide an interval. | 0 years 0 mons 0 days 0 hours 40 mins 0.0 secs |
Date/time field extraction and truncation
LAST_DAY
Description: Returns the last day of the month for a specified date. The supported time range is 1925 to 2282 by default.
LAST_DAY(DATE|TIMESTAMP|TIMESTAMPTZ)Usage notes:
This function is supported in the following Hologres versions:
NoteThis function does not support constant-only arguments in write operations.
V2.0.31 to V2.1.0
V2.1.13 and later
Return value:
Returns a
DATEvalue.Example:
Get the last day of the month for
2004-02-28 00:00:00.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;The following result is returned:
last_day ------------ 2004-02-29
ORACLE_LAST_DAY
Description: Returns the last day of the month for a specified date. This Oracle-compatible function requires the
orafceextension. Default time range: 1925 to 2282 year. For more information, see Supported Oracle functions.ORACLE_LAST_DAY(DATE)Return value:
Returns a
DATEvalue.Example:
SELECT ORACLE_LAST_DAY('2022-05-01');The following result is returned:
oracle_last_day ----------------- 2022-05-31
EXTRACT
Description: Extracts a subfield (such as year, month, or day) from a timestamp expression.
EXTRACT(field FROM TIMESTAMP)NoteValid values for the
fieldargument includecentury,day,decade,dow(day of week, Sunday is 0),isodow(day of week, Sunday is 7),doy(day of year),epoch,hour,minute,month,quarter,second,week, andyear.Return value
Returns a
DOUBLE PRECISIONvalue.Examples
Get the hour from a specified timestamp.
-- Returns: 20. SELECT EXTRACT(hour FROM timestamp '2001-02-16 20:38:40');Get the minute from the current time.
-- Returned result: 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;The following result is returned:
date_part ------------ 1001606400
Compatibility: Starting from Hologres V4.0, the following field extraction functions are supported for better compatibility with ClickHouse and Doris. These functions do not support constant-only arguments.
Function name
Return type
Description
extract_century(timestamp)
DOUBLE PRECISION
Same semantics as extract(century from timestamp), but with better performance.
extract_day(timestamp)
toDayOfMonth(timestamp)
day(timestamp)
DOUBLE PRECISION
Same semantics as extract(day from timestamp), but with better performance.
extract_decade(timestamp)
DOUBLE PRECISION
Same semantics as extract(decade from timestamp), but with better performance.
extract_dow(timestamp)
DOUBLE PRECISION
Same semantics as extract(dow from timestamp), but with better performance.
extract_doy(timestamp)
toDayOfYear(timestamp)
dayofyear(timestamp)
DOUBLE PRECISION
Same semantics as extract(doy from timestamp), but with better performance.
extract_hour(timestamp)
toHour(timestamp)
hour(timestamp)
DOUBLE PRECISION
Same semantics as extract(hour from timestamp), but with better performance.
extract_isodow(timestamp)
dayofweek_iso(timestamp)
DOUBLE PRECISION
Same semantics as extract(isodow from timestamp), but with better performance.
extract_minute(timestamp)
toMinute(timestamp)
minute(timestamp)
DOUBLE PRECISION
Same semantics as extract(minute from timestamp), but with better performance.
extract_month(timestamp)
toMonth(timestamp)
month(timestamp)
DOUBLE PRECISION
Same semantics as extract(month from timestamp), but with better performance.
extract_quarter(timestamp)
toQuarter(timestamp)
quarter(timestamp)
DOUBLE PRECISION
Same semantics as extract(quarter from timestamp), but with better performance.
extract_second(timestamp)
toSecond(timestamp)
second(timestamp)
DOUBLE PRECISION
Same semantics as extract(second from timestamp), but with better performance.
extract_isoweek(timestamp)
toIsoWeek(timestamp)
week_iso(timestamp)
DOUBLE PRECISION
Same semantics as extract(week from timestamp), but with better performance.
extract_year(timestamp)
toYear(timestamp)
year(timestamp)
DOUBLE PRECISION
Same semantics as extract(year from timestamp), but with better performance.
DATE_PART
Description: Extracts a specific part, such as the year, month, day, or hour part, from a timestamp. This function is equivalent to the EXTRACT function.
DATE_PART(<str> TEXT, <d> TIMESTAMP)Parameters:
str: The subfield to extract. Valid values include
century,day,decade,dow(day of week, Sunday is 0),isodow(day of week, Sunday is 7),doy(day of year),epoch,hour,minute,month,quarter,second,week, andyear.d: The date/time expression.
Return value:
Returns a
DOUBLE PRECISIONvalue.Examples:
Example 1: Get the hour from a specified timestamp.
SELECT DATE_PART('hour', timestamp '2001-02-16 16:38:40');The following result is returned:
date_part ----------- 16Example 2: Get the week number of the year for a specified date.
SELECT DATE_PART('week', TO_DATE('2022-10-11', 'YYYY-MM-DD'));The following result is returned:
date_part ----------- 41Example 3: Get the month number for a specified date.
SELECT DATE_PART('month', TO_DATE('2022-10-11', 'YYYY-MM-DD'));The following result is returned:
date_part ----------- 10
DATE_TRUNC
Description: Truncates a date or time value to a specified precision.
DATE_TRUNC(<str> TEXT, <d> TIME|TIMESTAMP|TIMESTAMPTZ)Parameters:
str: The precision to truncate to. Valid values include
century,decade,year,quarter,month,week,day,hour,minute, andsecond.d: The date or time value to be truncated.
Return value:
Returns a
TIMESTAMPorTIMESTAMPTZvalue.Examples:
Example 1: Truncate a specified time to the hour.
SELECT DATE_TRUNC('hour', time '12:38:40');The following result is returned:
date_trunc ------------ 12:00:00Example 2: Truncate a specified timestamp to the day.
SELECT DATE_TRUNC('day', timestamptz'2001-02-16 20:38:40+08');The following result is returned:
date_trunc ------------------------ 2001-02-16 00:00:00+08Example 3: Truncate a specified timestamp to the month.
SELECT DATE_TRUNC('month', timestamp '2001-02-16 18:38:40');The following result is returned:
date_trunc --------------------- 2001-02-01 00:00:00Example 4: Get the first day of the current month at 12:00.
SELECT DATE_TRUNC('month',now()) +interval '12h';The following result is returned:
?column? --------------------- 2024-08-01 12:00:00+08Example 5: Get 09:00 on the current day.
SELECT DATE_TRUNC('day',now()) + interval '9h';The following result is returned:
?column? ------------------------ 2024-08-08 09:00:00+08Example 6: Get the start of the same day of the week in the following week.
SELECT DATE_TRUNC('day',now()) + interval '7d';The following result is returned:
?column? ------------------------ 2024-08-15 00:00:00+08
TRUNC
Description: Truncates a date or timestamp to a specified precision. This Oracle-compatible function requires the
orafceextension. For more information, see Supported Oracle functions.TRUNC(<d> DATE|TIMESTAMP [, <str> TEXT])Parameters:
d: The date or time value to be truncated. If a
TIMESTAMPTZvalue is passed, the time part is set to00:00:00.str (Optional): The precision to truncate to. If omitted, the value is truncated to the day.
Yreturns the first day of the year, andQreturns the first day of the quarter. For more information, see the Oracle documentation.
Return value:
Returns a
DATEorTIMESTAMPvalue.Examples:
Example 1:
SELECT TRUNC('2022-05-22'::date,'Y');The following result is returned:
trunc ------------ 2022-01-01Example 2:
SELECT TRUNC('2022-05-22 13:11:22'::timestamp,'Y');The following result is returned:
trunc --------------------- 2022-01-01 00:00:00Example 3:
SELECT TRUNC('2022-05-22 13:11:22'::timestamp,'Q');The following result is returned:
trunc --------------------- 2022-04-01 00:00:00Example 4:
SELECT TRUNC('2022-05-22 13:11:22'::timestamp);The following result is returned:
trunc --------------------- 2022-05-22 00:00:00
ROUND
Description: Rounds a date or time value to the nearest time unit. This Oracle-compatible function requires the
orafceextension. For more information, see Supported Oracle functions.ROUND(<d> DATE|TIMESTAMPTZ [, <str> TEXT])Parameters:
d: The date or time value to be rounded.
str: (Optional): The time unit to round to. If omitted, the value is rounded to the nearest day.
Yrounds to the first day of the nearest year. For more information, see the Oracle documentation.
Return value:
Returns a
DATEorTIMESTAMPvalue.Examples:
Example 1:
SELECT ROUND('2022-05-22'::date,'Y');The following result is returned:
round ------------ 2022-01-01Example 2:
SELECT ROUND('2022-07-22'::date,'Y');The following result is returned:
round ------------ 2023-01-01Example 3:
SELECT ROUND('2022-07-22 13:11:22'::timestamp,'Y');The following result is returned:
round --------------------- 2023-01-01 00:00:00Example 4:
SELECT ROUND('2022-02-22 13:11:22'::timestamp);The following result is returned:
round --------------------- 2022-02-23 00:00:00
Current date/time functions
CURRENT_DATE
Description: Returns the current date at the start of the transaction.
CURRENT_DATEReturn value:
Returns a
DATEvalue.Example:
SELECT CURRENT_DATE;The following result is returned:
current_date -------------- 2024-08-08
CURRENT_TIMESTAMP
Description: Returns the start time of the current transaction. This function is equivalent to
TRANSACTION_TIMESTAMP()andNOW().CURRENT_TIMESTAMPNoteThe returned value does not change within a single transaction.
Return value:
Returns a
TIMESTAMPTZvalue.Example:
SELECT CURRENT_TIMESTAMP;The following result is returned:
current_timestamp ------------------------------- 2024-08-08 14:55:11.006068+08
CLOCK_TIMESTAMP
Description: Returns the actual current time.
clock_timestamp()NoteThe returned value changes on each call, even within a single SQL statement.
Return value:
Returns a
TIMESTAMPTZvalue.Example:
SELECT clock_timestamp();The following result is returned:
clock_timestamp ------------------------------- 2024-08-08 14:57:43.569109+08
LOCALTIMESTAMP
Description: Returns the start time of the current transaction, but without time zone information.
LOCALTIMESTAMPReturn value:
Returns a
TIMESTAMPvalue.Example:
SELECT LOCALTIMESTAMP;The following result is returned:
localtimestamp --------------------------- 2024-08-08 15:00:59.13245
NOW
Description: Returns the start time of the current transaction. This function is equivalent to
TRANSACTION_TIMESTAMP()andCURRENT_TIMESTAMP.NOW()NoteThe returned value does not change within a single transaction.
Return value:
Returns a
TIMESTAMPTZvalue.Example:
SELECT NOW();The following result is returned:
now ------------------------------- 2024-08-08 15:02:50.270501+08
STATEMENT_TIMESTAMP
Description: Returns the start time of the current statement.
STATEMENT_TIMESTAMP()NoteThe return value is consistent within a single statement but differs across multiple statements within the same transaction.
Return value:
Returns a
TIMESTAMPTZvalue.Example:
SELECT STATEMENT_TIMESTAMP();The following result is returned:
statement_timestamp ------------------------------- 2024-08-08 15:06:14.772939+08
TIMEOFDAY
Description: Returns the actual current time, similar to
CLOCK_TIMESTAMP(). However,TIMEOFDAY()returns the time as a formatted text string.TIMEOFDAY()Return value:
Returns a
TEXTvalue.Example:
SELECT TIMEOFDAY();The following result is returned:
timeofday ------------------------------------- Thu Aug 08 15:08:16.599369 2024 CST
TRANSACTION_TIMESTAMP
Description: Returns the start time of the current transaction. This function is equivalent to
CURRENT_TIMESTAMPandNOW().TRANSACTION_TIMESTAMP()NoteThe returned value does not change within a single transaction.
Return value:
Returns a
TIMESTAMPTZvalue.Example:
SELECT TRANSACTION_TIMESTAMP();The following result is returned:
transaction_timestamp ------------------------------- 2024-08-08 15:11:10.329005+08
Other functions
ISFINITE
Description:
Tests if a date is finite (i.e., not
infinityor-infinity).ISFINITE(DATE)Tests if a timestamp is finite.
ISFINITE(TIMESTAMP)
Return value:
Returns a
BOOLEANvalue. Returnstrue(t) if the value is finite, andfalse(f) if it is infinite.Examples:
Example 1:
SELECT ISFINITE(date '2001-02-16');The following result is returned:
isfinite ---------- tExample 2:
SELECT ISFINITE(timestamp '2001-02-16 21:28:30');The following result is returned:
isfinite ---------- t
Examples
Example 1: Add several hours to the current time.
SELECT NOW()+interval '2 hour';The following result is returned:
?column? --------------------- 2022-12-29 13:43:58.321104+08Example 2: Convert a date into a timestamp.
SELECT EXTRACT(epoch FROM current_timestamp);The following result is returned:
date_part --------------------- 1672285506.296279Example 3: Add a value of the DATE type and a value of the INT type.
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;The following result is returned:
?column? -------------------- 2022-09-28 00:00:00Example 4: Convert a string into a timestamp.
SELECT TO_TIMESTAMP(TO_CHAR(20211027172045,'9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS');The following result is returned:
to_timestamp ---------------------- 2021-10-27 17:20:45+08Example 5: Truncating time
SELECT EXTRACT(mon FROM now());The following result is returned:
date_part --------- 12Example 6: Divide an integer by another integer.
When you divide two integers, Hologres performs integer division and discards the remainder. For example, the result of
10/3is 3. To obtain a result with a decimal part, explicitly cast the data type to float before the calculation, as shown in the following example:SELECT 10/3::float;The following result is returned:
?column? --------- 3.3333333333333335