All Products
Search
Document Center

Hologres:Date and time functions

Last Updated:Dec 02, 2024

This topic describes the date and time functions that are supported by Hologres and provides examples on how to use the functions.

Type

Function

Description

Data type conversion functions

MAKE_DATE

Creates a date that consists of the year, month, and day. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

TO_CHAR

Converts a timestamp, an integer, a real number, or a double-precision number into a string.

TO_DATE

Converts a string into a date. By default, only strings that describe dates in a year from 1925 to 2282 can be converted.

TO_TIMESTAMP

Converts a string into a timestamp or a timestamp into a date.

Functions and operators for basic operations on date and time values

ADD_MONTHS

Adds a specific number of months to a specified date. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

DATEADD

Adds or subtracts an interval to or from a specified date and time value based on a specified time unit. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

DATEDIFF

Calculates the difference between two dates or timestamps. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

MONTHS_BETWEEN

Returns the number of months between two dates. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

NEXT_DAY

Returns the date of the first specified day of a week after a specified date. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

Date or time addition: the operator +

Adds days or time.

Date or time subtraction: the operator -

Subtracts days or time.

Date or time multiplication: the operator *

Multiplies days or time.

Date or time division: the operator /

Divides days or time.

Date and time truncation functions

DATE_PART

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_TRUNC

Truncates date and time data to a specified time unit.

EXTRACT

Extracts a specific part, such as the year, month, day, or hour part, from a timestamp.

LAST_DAY

Returns the last day of the month in which a date value falls. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

ORACLE_LAST_DAY

Returns the last day of the month in which a date value falls. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

ROUND

Rounds a date to the nearest value based on a time unit. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

TRUNC

Truncates a date or timestamp to a specified time unit. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

Current date and time acquisition functions

CLOCK_TIMESTAMP

Returns the current date and time.

CURRENT_DATE

Returns the current date.

CURRENT_TIMESTAMP

Returns the start time of the current transaction. This function is equivalent to the TRANSACTION_TIMESTAMP or NOW function.

LOCALTIMESTAMP

Returns the current time that does not contain the time zone information.

NOW

Returns the start time of the current transaction. This function is equivalent to the TRANSACTION_TIMESTAMP or CURRENT_TIMESTAMP function.

STATEMENT_TIMESTAMP

Returns the start time of the current statement.

TIMEOFDAY

Returns the current date and time. This function is similar to the CLOCK_TIMESTAMP function. The value returned by the TIMEOFDAY function is a formatted text string.

TRANSACTION_TIMESTAMP

Returns the start time of the current transaction. This function is equivalent to the CURRENT_TIMESTAMP or NOW function.

Others

ISFINITE

Checks whether a date of the DATE type is a finite number.

Date and time conversion functions

TO_CHAR

  • Description:

    • Converts a timestamp into a string. By default, only timestamps that describe time points in a year from 1925 to 2282 can be converted.

      TO_CHAR(TIMESTAMP|TIMESTAMPTZ, TEXT)

      Usage notes:

      • You can use this function to convert timestamps into strings in the 24-hour clock or the 12-hour clock. HH24 indicates the 24-hour clock, and HH12 indicates the 12-hour clock. By default, the 12-hour clock is used.

      • YYYY indicates the year, MM indicates the month, DD indicates the day, HH indicates the hour, MI indicates the minute, and SS indicates the second.

      • In Hologres V1.1.31 and later, you can execute the set hg_experimental_functions_use_pg_implementation = 'to_char'; or set hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp'; statement before you call this function in an SQL statement. This allows the function to support date and time values in any year.

        Note

        If 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%.

    • Converts an integer into a string.

      TO_CHAR(INT, TEXT)
    • Converts a real number or a double-precision number into a string.

      TO_CHAR(DOUBLE PRECISION, TEXT)
  • Return value:

    A value of the TEXT type is returned.

  • Examples:

    • Convert a timestamp into a string in the 24-hour clock.

      -- 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 into a string in the 12-hour clock.

      -- 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 value of a field into a string.

      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 value of the TEXT type 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 into a string in another time zone.

      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 timestamp value of the a field is converted into a string that uses the UTC+8 time zone.

        -- Returned result: 2023-03-21 16:23:54.
        SELECT TO_CHAR(a, 'YYYY-MM-DD HH24:MI:SS') FROM timestamptz_test;
      • Convert the timestamp value of the a field into a string that uses the UTC-5 time zone.

        -- 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 into a string.

      -- Returned result: 125.
      SELECT TO_CHAR(125, '999');
    • Convert a double-precision number into 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:

    In Hologres V1.1.31 and later, you can execute the set hg_experimental_functions_use_pg_implementation = 'to_date'; or set hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp'; statement before you call this function in an SQL statement. This allows the function to support date and time values in any year.

    Note

    If 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 into a timestamp. By default, only strings that describe time points in a year from 1925 to 2282 can be converted.

      TO_TIMESTAMP(<text_date> TEXT, <format_mask> TEXT)
      • Usage notes:

        • The return value contains +08.

        • In Hologres V1.1.31 and later, you can execute the set hg_experimental_functions_use_pg_implementation = 'to_timestamp'; or set hg_experimental_functions_use_pg_implementation = 'to_char,to_date,to_timestamp'; statement before you call this function in an SQL statement. This allows the function to support date and time values in any year.

          Note

          If 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 into a timestamp.

        • format_mask: required. The timestamp format.

    • Converts a timestamp into a date.

      TO_TIMESTAMP(DOUBLE PRECISION)
      Note

      The UNIX timestamp represents the number of seconds that have elapsed since 00:00:00, January 1, 1970.

  • Return value:

    A value of the TIMESTAMPTZ type is returned.

  • Examples:

    • Convert a string into a timestamp.

      SELECT TO_TIMESTAMP('05 Dec 2000', 'DD Mon YYYY');

      The following result is returned:

            to_timestamp
      ------------------------
       2000-12-05 00:00:00+08
    • Convert a string of the TEXT data type into 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+08
    • Convert a UNIX timestamp in seconds into a date.

      -- Returned result: 1975-03-06 03:38:16+08.
      SELECT TO_TIMESTAMP(163280296);
    • Convert a UNIX timestamp in milliseconds into a date.

      -- Returned result: 2021-09-28 12:22:41+08.
      SELECT TO_TIMESTAMP(1632802961000/1000);

MAKE_DATE

  • Description: Creates a date that consists of the year, month, and day. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

    MAKE_DATE(<year> INT, <month> INT, <day> INT)
  • Usage notes:

    Hologres V2.0.29 and later support this function. This function does not support constants as input parameters.

  • Return value:

    A value of the DATE type is returned.

  • Example:

    -- Returned result: 2013-07-15.
    SELECT MAKE_DATE(2013, 7, 15);

    The following result is returned:

    make_date
    ------------
     2013-07-15

Functions and operators for basic operations on date and time values

DATEADD

  • Description: Adds or subtracts an interval to or from a specified date and time value based on a specified time unit. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

    DATEADD(<d> DATE|TIMESTAMP|TIMESTAMPTZ, <num> BIGINT, <str> TEXT)
  • Usage notes:

    Hologres instances of the following versions support this function:

    • V2.0.31 to V2.1.0

    • V2.1.13 and later

    Note

    This function does not support constants as input parameters.

  • Parameters:

    • d: required. The original date and time value.

    • num: required. The interval that you want to add or subtract.

    • str: required. The specified time unit. Valid values: yyyy, year, mm, month, mon, dd, day, hh, hour, mi, and ss.

  • Return value:

    A value of the DATE, TIMESTAMP, or TIMESTAMPTZ type is returned.

  • 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 specific number of months to a specified date and time value. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

    ADD_MONTHS(<d> DATE, <month> INT)
  • Parameters:

    • d: required. The original date.

    • month: required. The integer that you want to add to the original date.

  • Return value:

    A value of the DATE type is returned.

  • Example:

    SELECT ADD_MONTHS(current_date, 2);

    The following result is returned:

     add_months
    ------------
     2024-10-05

DATEDIFF

  • Description: Calculates the difference between two dates or timestamps. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

    DATEDIFF(<d1> DATE|TIMESTAMP|TIMESTAMPTZ, <d2> DATE|TIMESTAMP|TIMESTAMPTZ, <str> TEXT)
  • Usage notes:

    Hologres instances of the following versions support this function:

    • V2.0.31 to V2.1.0

    • V2.1.13 and later

    Note

    Input parameters of this function cannot be all constants.

  • Parameters:

    • d1: required. The first date or timestamp.

    • d2: required. The second date or timestamp.

    • str: required. The time unit based on which the time difference is calculated. Valid values: yyyy, year, mm, month, mon, dd, day, hh, hour, mi, and ss.

  • Return value:

    A value of the BIGINT type is returned. If the time difference in the specified time unit is less than 1, the value 0 is returned by default.

    For example, if you use this function to calculate the difference between 2023-12-31 and 2024-01-01 based on the time unit year, the value 0 is returned.

    Note

    If you want this function to return 1 in the preceding scenario, you can execute the set hg_experimental_datediff_use_presto_impl = off; statement before the SQL statement in which the function is included.

  • 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: Returns the number of months between two dates. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

    MONTHS_BETWEEN(DATE, DATE)
  • Return value:

    A value of the INT type is returned.

  • Examples:

    • Example 1:

      -- Returned result: 2.
      SELECT MONTHS_BETWEEN('2022-01-01', '2021-11-01');
    • Example 2:

      -- Returned result: -2.
      SELECT MONTHS_BETWEEN('2021-11-01', '2022-01-01');

NEXT_DAY

  • Description: Returns the date of the first specified day of a week after a specified date. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

    NEXT_DAY(<d> DATE, <str> TEXT|INT)
  • Parameters:

    • d: required. The specified date.

    • str: required. A string that represents the day of a week. Example: Friday. This parameter can also be set to a number that represents the day of a week. The number ranges from 1 to 7. The value 1 indicates Sunday, and the value 2 indicates Monday. In a similar manner, the value 7 indicates Saturday.

  • Return value:

    A value of the DATE type is returned.

  • 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);

Date or time addition: the operator +

Return value type

Example

Returned result

DATE

Add seven days to a specified date.

SELECT date '2001-09-28' + integer '7';

2001-10-05

Add three days to the current date.

SELECT current_date+ integer '3 ';

2022-12-10

Add one day to the current time.

SELECT to_char(current_date+ interval '1 day','yyyy-mm-dd');

2022-12-09

TIMESTAMP

Add three hours to a specified date. The time starts from 00:00:00.

SELECT date '2001-09-28' + time '03:00';

2001-09-28 03:00:00

Add one hour to a specified date. The time starts from 00:00:00.

SELECT date '2001-09-28' + interval '1 hour';

2001-09-28 01:00:00

TIMESTAMPTZ

Add one day to the current time.

SELECT now()+interval '1 day';

2022-12-08 20:09:19.388465+08

Add one month to the current time.

SELECT now()+interval '1 month';

2023-01-08 20:21:50.993481+08

Add two years to the current time.

 SELECT now()+interval '2 year';

2024-12-08 20:22:49.416343+08

Date or time subtraction: the operator -

Return value type

Example

Returned result

INTEGER

Subtract a specified date from another specified date.

SELECT date '2001-10-01' - date '2001-09-28';

3

DATE

Subtract seven days from a specified date.

SELECT date '2001-10-01' - integer '7';

2001-09-24

TIMESTAMP

Subtract three hours from a specified date.

SELECT date '2001-09-28' - time '03:00';

2001-09-27 21:00:00

Subtract one hour from a specified date.

 SELECT date '2001-09-28' - interval '1 hour';

2001-09-27 23:00:00

Subtract two days from the current time.

SELECT now()-interval '2 day';

2022-12-06 20:27:21.094258+08

Date or time multiplication: the operator *

Return value type

Example

Returned result

INTERVAL

Multiply a number by another number.

SELECT 21 * interval '3 day';

0 years 0 mons 63 days 0 hours 0 mins 0.0 secs

Date or time division: the operator /

Return value type

Example

Returned result

INTERVAL

Divide a number by another number.

SELECT interval '1 hour' / double precision '1.5';

0 years 0 mons 0 days 0 hours 40 mins 0.0 secs

Date and time truncation functions

LAST_DAY

  • Description: Returns the last day of the month in which a date value falls. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported.

    LAST_DAY(DATE|TIMESTAMP|TIMESTAMPTZ)
  • Usage notes:

    Hologres instances of the following versions support this function:

    Note

    This function does not support constants as input parameters.

    • V2.0.31 to V2.1.0

    • V2.1.13 and later

  • Return value:

    A value of the DATE type is returned.

  • Example:

    Extract the last day of the month in which 2004-02-28 00:00:00 falls.

    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 in which a date value falls. By default, only date and time values that describe time points in a year from 1925 to 2282 are supported. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

    ORACLE_LAST_DAY(DATE)
  • Return value:

    A value of the DATE type is returned.

  • Example:

    SELECT ORACLE_LAST_DAY('2022-05-01');

    The following result is returned:

     oracle_last_day
    -----------------
     2022-05-31

EXTRACT

  • Description: Extracts a specific part, such as the year, month, day, or hour part, from a timestamp.

    EXTRACT(field FROM TIMESTAMP)
    Note

    The input constants of the field parameter include century, day, decade, dow (the day of the week, with Sunday being 0), isodow (the day of the week, with Sunday being 7), doy (the day of the year), epoch (Unix epoch), hour, minute, month, quarter, second, week, and year.

  • Return value:

    A value of the DOUBLE PRECISION type is returned.

  • Examples:

    • Extract the hour part from a timestamp.

      -- Returned result: 20.
      SELECT EXTRACT(hour FROM timestamp '2001-02-16 20:38:40');
    • Extract the minute part from the current time.

      -- Returned result: 12.
      SELECT EXTRACT(minute FROM NOW());
    • Extract the number of seconds from 1970 to the time specified by the value of a field.

      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

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: required. The part that you want to extract. Valid values: century, day, decade, dow (the day of the week, with Sunday being 0), isodow (the day of the week, with Sunday being 7), doy (the day of the year), epoch (the number of days since the Unix epoch), hour, minute, month, quarter, second, week, and year.

    • d: required. The date and time expression.

  • Return value:

    A value of the DOUBLE PRECISION type is returned.

  • Examples:

    • Example 1: Extract the hour part from a timestamp.

      SELECT DATE_PART('hour', timestamp '2001-02-16 16:38:40');

      The following result is returned:

       date_part
      -----------
              16
    • Example 2: Extract the number of weeks from January 1 to a specified date.

      SELECT DATE_PART('week', TO_DATE('2022-10-11', 'YYYY-MM-DD'));

      The following result is returned:

       date_part
      -----------
              41
    • Example 3: Extract the number of months from January 1 to 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 date and time data to a specified time unit.

    DATE_TRUNC(<str> TEXT, <d> TIME|TIMESTAMP|TIMESTAMPTZ)
  • Parameters:

    • str: required. The time unit. Valid values: century, decade, year, quarter, month, week, day, hour, minute, and second.

    • d: required. The date and time data that you want to truncate.

  • Return value:

    A value of the TIMESTAMP or TIMESTAMPTZ type is returned.

  • Examples:

    • Example 1: Truncate a specified timestamp to the hour part.

      SELECT DATE_TRUNC('hour', time '12:38:40');

      The following result is returned:

       date_trunc
      ------------
       12:00:00
    • Example 2: Truncate a specified timestamp to the day part.

      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+08
    • Example 3: Truncate a specified timestamp to the month part.

      SELECT DATE_TRUNC('month', timestamp '2001-02-16 18:38:40');

      The following result is returned:

           date_trunc
      ---------------------
       2001-02-01 00:00:00
    • Example 4: Truncate the current timestamp to the month part and add 12 hours to return 12:00:00 on the first day of the current month.

      SELECT DATE_TRUNC('month',now()) +interval '12h';

      The following result is returned:

              ?column?
      ---------------------
       2024-08-01 12:00:00+08
    • Example 5: Truncate the current timestamp to the day part and add 9 hours to return 09:00:00 on the current day.

      SELECT DATE_TRUNC('day',now()) + interval '9h';

      The following result is returned:

              ?column?
      ------------------------
       2024-08-08 09:00:00+08
    • Example 6: Truncate the current timestamp to the day part and add 7 days to return the current day of the next 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 time unit. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

    TRUNC(<d> DATE|TIMESTAMP [, <str> TEXT])
  • Parameters:

    • d: required. The date and time value that you want to truncate. If you specify a value of the TIMESTAMPTZ type, the remaining part is set to 0.

    • str: optional. The time unit. If you do not configure this parameter, the original date is returned. You can set this parameter to Y or Q. The value Y indicates the first day of the year, and the value Q indicates the first day of the quarter. For more information, see the Oracle documentation.

  • Return value:

    A value of the DATE or TIMESTAMPTZ type is returned.

  • Examples:

    • Example 1:

      SELECT TRUNC('2022-05-22'::date,'Y');

      The following result is returned:

         trunc
      ------------
       2022-01-01
    • Example 2:

      SELECT TRUNC('2022-05-22 13:11:22'::timestamp,'Y');

      The following result is returned:

              trunc
      ---------------------
       2022-01-01 00:00:00
    • Example 3:

      SELECT TRUNC('2022-05-22 13:11:22'::timestamp,'Q');

      The following result is returned:

              trunc
      ---------------------
       2022-04-01 00:00:00
    • Example 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 to the nearest value based on a time unit. This function is compatible with Oracle. Before you use this function, you must install the Orafce extension. For more information, see Supported Oracle functions.

    ROUND(<d> DATE|TIMESTAMPTZ [, <str> TEXT])
  • Parameters:

    • d: required. The date that you want to round. If you specify a value of the TIMESTAMPTZ type, the remaining part is set to 0.

    • str: optional. The time unit. If you do not configure this parameter, this function rounds a date to the nearest day. You can set this parameter to Y, which indicates the first day of the nearest year. For more information, see the Oracle documentation.

  • Return value:

    A value of the DATE or TIMESTAMPTZ type is returned.

  • Examples:

    • Example 1:

      SELECT ROUND('2022-05-22'::date,'Y');

      The following result is returned:

         round
      ------------
       2022-01-01
    • Example 2:

      SELECT ROUND('2022-07-22'::date,'Y');

      The following result is returned:

         round
      ------------
       2023-01-01
    • Example 3:

      SELECT ROUND('2022-07-22 13:11:22'::timestamp,'Y');

      The following result is returned:

              round
      ---------------------
       2023-01-01 00:00:00
    • Example 4:

      SELECT ROUND('2022-02-22 13:11:22'::timestamp);

      The following result is returned:

              round
      ---------------------
       2022-02-23 00:00:00

Current date and time acquisition functions

CURRENT_DATE

  • Description: Returns the current date.

    CURRENT_DATE
  • Return value:

    A value of the DATE type is returned.

  • 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 the TRANSACTION_TIMESTAMP or NOW function.

    CURRENT_TIMESTAMP
    Note

    The return value remains unchanged during the lifecycle of the transaction.

  • Return value:

    A value of the TIMESTAMPTZ type is returned.

  • Example:

    SELECT CURRENT_TIMESTAMP;

    The following result is returned:

           current_timestamp
    -------------------------------
     2024-08-08 14:55:11.006068+08

CLOCK_TIMESTAMP

  • Description: Returns the current date and time.

    clock_timestamp()
    Note

    This function returns a different value each time even the function is used in the same statement.

  • Return value:

    A value of the TIMESTAMPTZ type is returned.

  • Example:

    SELECT clock_timestamp();

    The following result is returned:

            clock_timestamp
    -------------------------------
     2024-08-08 14:57:43.569109+08

LOCALTIMESTAMP

  • Description: Returns the current time that does not contain the time zone information.

    LOCALTIMESTAMP
  • Return value:

    A value of the TIMESTAMPTZ type is returned.

  • 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 the TRANSACTION_TIMESTAMP or CURRENT_TIMESTAMP function.

    NOW()
    Note

    The return value remains unchanged during the lifecycle of the transaction.

  • Return value:

    A value of the TIMESTAMPTZ type is returned.

  • 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()
    Note

    The return value varies based on the statement of the transaction in which the function is used.

  • Return value:

    A value of the TIMESTAMPTZ type is returned.

  • Example:

    SELECT STATEMENT_TIMESTAMP();

    The following result is returned:

          statement_timestamp
    -------------------------------
     2024-08-08 15:06:14.772939+08

TIMEOFDAY

  • Description: Returns the current date and time. This function is similar to the CLOCK_TIMESTAMP function. The value returned by the TIMEOFDAY function is a formatted text string.

    TIMEOFDAY()
  • Return value:

    A value of the TEXT type is returned.

  • 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 the CURRENT_TIMESTAMP or NOW function.

    TRANSACTION_TIMESTAMP()
    Note

    The return value remains unchanged during the lifecycle of the transaction.

  • Return value:

    A value of the TIMESTAMPTZ type is returned.

  • Example:

    SELECT TRANSACTION_TIMESTAMP();

    The following result is returned:

         transaction_timestamp
    -------------------------------
     2024-08-08 15:11:10.329005+08

Other functions

ISFINITE

  • Description:

    • Checks whether a date of the DATE type is a finite number.

      ISFINITE(DATE)
    • Checks whether a timestamp is a finite number.

      ISFINITE(TIMESTAMP)
  • Return value:

    A value of the BOOLEAN type is returned. The value true (t) is returned if the date or timestamp is a finite number, and the value false (f) is returned if the date or timestamp is not a finite number.

  • Examples:

    • Example 1:

      SELECT ISFINITE(date '2001-02-16');

      The following result is returned:

       isfinite
      ----------
       t
    • Example 2:

      SELECT ISFINITE(timestamp '2001-02-16 21:28:30');

      The following result is returned:

       isfinite
      ----------
       t

Common sample SQL statements

  • 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+08
  • Example 2: Convert a date into a timestamp.

    SELECT EXTRACT(epoch FROM current_timestamp);

    The following result is returned:

    date_part
    ---------------------
    1672285506.296279
  • Example 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:00
  • Example 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+08
  • Example 5: Extract the month part.

    SELECT EXTRACT(mon FROM now());

    The following result is returned:

    date_part
    ---------
    12
  • Example 6: Divide an integer by another integer.

    If a remainder exists after you divide an integer by another integer, Hologres returns an integer and discards the remainder. For example, the return value of 10/3 is 3. If the remainder needs to be displayed, you can perform an explicit data type conversion. In this example, you can convert the data type to FLOAT before calculation.

    SELECT 10/3::float;

    The following result is returned:

    ?column?
    ---------
    3.3333333333333335