TO_DSINTERVAL

2024-04-19 08:42:51  Updated

Purpose

TO_DSINTERVAL() converts a string of the CHAR, VARCHAR2, NCHAR, or NVARCHAR2 data type to a value of the INTERVAL DAY TO SECOND data type. This function can be used to perform addition and subtraction operations on a datetime value.

Syntax

/*SQL date format*/
TO_DSINTERVAL ('[+ | -] days hours:minutes:seconds[.frac_secs]')

/*ISO date format*/
TO_DSINTERVAL ('[-] P[days D]
[T[hours H][minutes M][seconds[.frac_secs]S]]')

Parameters

Parameter Description
[+ | -]days hours:minutes:seconds[.frac_secs] A string of the CHAR, VARCHAR2, NCHAR, or NVARCHAR2 data type in the acceptable format.
  • days specifies the day. The value is an integer in the range of [0, 999999999].
  • hours specifies the hour. The value is an integer in the range of [0, 23].
  • minutes specifies the minute. The value is an integer in the range of [0, 59].
  • seconds specifies the second. The value is an integer in the range of [0, 59].
[-] P[days D] [T[hours H][minutes M][seconds[.frac_secs]S] A string of the CHAR, VARCHAR2, NCHAR, or NVARCHAR2 data type in the acceptable format.
Notice: Blank spaces are not allowed in the value.
frac_secs Optional. Specifies the fractional part of seconds. The value is an integer in the range of [0, 999999999].

Return type

The return type is INTERVAL DAY TO SECOND.

Examples

  • The following example returns the datetime value of 100 days after November 11, 2021 in SQL format.

    obclient> SELECT TO_CHAR(TIMESTAMP '2021-11-11 00:00:00'+TO_DSINTERVAL('100 00:00:00'),
         'YYYY-MM-DD HH24:MI:SS') "Time Stamp" FROM DUAL;
    +---------------------+
    | Time Stamp          |
    +---------------------+
    | 2022-02-19 00:00:00 |
    +---------------------+
    1 row in set
    
  • The following example returns the datetime value of 100 days and 10 hours after December 12, 2021 in ISO format.

    obclient> SELECT TO_CHAR(TIMESTAMP '2021-11-11 00:00:00'+TO_DSINTERVAL('P100DT5H'),
        'YYYY-MM-DD HH24:MI:SS') "Time Stamp" FROM DUAL;
    +---------------------+
    | Time Stamp          |
    +---------------------+
    | 2022-02-19 05:00:00 |
    +---------------------+
    1 row in set
    

Contact Us