|
| 1 | +.. _intervaltype: |
| 2 | + |
| 3 | +******************* |
| 4 | +Using INTERVAL Data |
| 5 | +******************* |
| 6 | + |
| 7 | +Oracle Database supports two INTERVAL data types that store time durations - |
| 8 | +INTERVAL YEAR TO MONTH and INTERVAL DAY TO SECOND. For more information on |
| 9 | +these data types, see `Oracle Interval Types <https://www.oracle.com/pls/topic |
| 10 | +/lookup?ctx=dblatest&id=GUID-7690645A-0EE3-46CA-90DE-C96DF5A01F8F>`__. |
| 11 | + |
| 12 | +Node-oracledb does not support using INTERVAL data types in |
| 13 | +:ref:`Oracle Database Objects <dbobjectclass>`. |
| 14 | + |
| 15 | +.. _intervalyeartomonth: |
| 16 | + |
| 17 | +Using INTERVAL YEAR TO MONTH Data |
| 18 | +================================= |
| 19 | + |
| 20 | +The INTERVAL YEAR TO MONTH data type stores a period of time using years and |
| 21 | +months. |
| 22 | + |
| 23 | +To create a table with a column for INTERVAL YEAR TO MONTH data, for example: |
| 24 | + |
| 25 | +.. code-block:: sql |
| 26 | +
|
| 27 | + CREATE TABLE TableIntervalYM (IntervalCol INTERVAL YEAR TO MONTH); |
| 28 | +
|
| 29 | +.. _insertintervalyeartomonth: |
| 30 | + |
| 31 | +Inserting INTERVAL YEAR TO MONTH |
| 32 | +-------------------------------- |
| 33 | + |
| 34 | +You must create an IntervalYM object using :ref:`intervalymclass` to insert |
| 35 | +into an INTERVAL YEAR TO MONTH column. For example: |
| 36 | + |
| 37 | +.. code-block:: javascript |
| 38 | +
|
| 39 | + const interval = new oracledb.IntervalYM(); |
| 40 | +
|
| 41 | +This creates an Oracledb IntervalYM object with ``years`` and ``months`` |
| 42 | +attributes set to *0*. You can also create an IntervalYM object with zero |
| 43 | +intervals using: |
| 44 | + |
| 45 | +.. code-block:: javascript |
| 46 | +
|
| 47 | + const interval = oracledb.IntervalYM({}); |
| 48 | +
|
| 49 | +You can define the optional ``years`` and ``months`` attributes in the |
| 50 | +IntervalYM class. For example, to create an Oracledb IntervalYM cobject with |
| 51 | +only the ``years`` attribute set to *2*: |
| 52 | + |
| 53 | +.. code-block:: javascript |
| 54 | +
|
| 55 | + const interval = new oracledb.IntervalYM({ years: 2 }); |
| 56 | +
|
| 57 | +An example of creating an IntervalYM object with ``years`` and ``months`` |
| 58 | +attributes set to *1* and *6* respectively is shown below. This example is |
| 59 | +used in subsequent sections. |
| 60 | + |
| 61 | +.. code-block:: javascript |
| 62 | +
|
| 63 | + const interval = new oracledb.IntervalYM({ years: 1, months: 6 }); |
| 64 | +
|
| 65 | +If you specify non-integer values in the attributes of IntervalYM object, then |
| 66 | +the ``NJS-007`` error is raised. |
| 67 | + |
| 68 | +You can insert IntervalYM objects into an INTERVAL YEAR TO MONTH column by |
| 69 | +binding as ``oracledb.DB_TYPE_YM``, for example: |
| 70 | + |
| 71 | +.. code-block:: javascript |
| 72 | +
|
| 73 | + await connection.execute( |
| 74 | + `INSERT INTO TableIntervalYM VALUES (:bv)`, |
| 75 | + { bv: { val: interval, type: oracledb.DB_TYPE_YM } |
| 76 | + ); |
| 77 | +
|
| 78 | +.. _fetchintervalyeartomonth: |
| 79 | +
|
| 80 | +Fetching INTERVAL YEAR TO MONTH |
| 81 | +------------------------------- |
| 82 | +
|
| 83 | +To query an INTERVAL YEAR TO MONTH column, you can use: |
| 84 | +
|
| 85 | +.. code-block:: javascript |
| 86 | +
|
| 87 | + const result = await connection.execute(`SELECT * FROM TableIntervalYM`); |
| 88 | + console.log(result.rows[0][0]); |
| 89 | +
|
| 90 | +This query prints:: |
| 91 | +
|
| 92 | + IntervalYM { years: 1, months: 6 } |
| 93 | +
|
| 94 | +.. _intervaldaytosecond: |
| 95 | +
|
| 96 | +Using INTERVAL DAY TO SECOND Data |
| 97 | +================================= |
| 98 | +
|
| 99 | +The INTERVAL DAY TO SECOND data type stores a period of time using days, |
| 100 | +hours, minutes, seconds, and fractional seconds. |
| 101 | +
|
| 102 | +To create a table with a column for INTERVAL DAY TO SECOND data, for example: |
| 103 | +
|
| 104 | +.. code-block:: sql |
| 105 | +
|
| 106 | + CREATE TABLE TableIntervalDS (IntervalDSCol INTERVAL DAY TO SECOND); |
| 107 | +
|
| 108 | +.. _insertintervaldaytosecond: |
| 109 | +
|
| 110 | +Inserting INTERVAL DAY TO SECOND |
| 111 | +-------------------------------- |
| 112 | +
|
| 113 | +You must create an IntervalDS object using :ref:`intervaldsclass` to insert |
| 114 | +into an INTERVAL DAY TO SECOND column. For example: |
| 115 | +
|
| 116 | +.. code-block:: javascript |
| 117 | +
|
| 118 | + const interval = oracledb.IntervalDS(); |
| 119 | +
|
| 120 | +This creates an Oracledb IntervalDS object with ``days``, ``hours``, |
| 121 | +``minutes``, ``seconds``, and ``fseconds`` (fractional seconds) attributes set |
| 122 | +to *0*. You can also create an IntervalDS object with zero intervals using: |
| 123 | +
|
| 124 | +.. code-block:: javascript |
| 125 | +
|
| 126 | + const interval = oracledb.IntervalDS({}); |
| 127 | +
|
| 128 | +You can define the optional ``days``, ``hours``, ``minutes``, ``seconds``, and |
| 129 | +``fseconds`` attributes in the IntervalDS object. For example, to create an |
| 130 | +Oracledb IntervalDS object with the ``days``and ``seconds`` attributes set to |
| 131 | +*2* and *40* respectively is shown below. This example is used in subsequent |
| 132 | +sections. |
| 133 | +
|
| 134 | +.. code-block:: javascript |
| 135 | +
|
| 136 | + const data = new oracledb.IntervalDS({ days: 2, seconds: 40 }); |
| 137 | +
|
| 138 | +If you specify non-integer values in the attributes of IntervalDS object, then |
| 139 | +the ``NJS-007`` error is raised. |
| 140 | +
|
| 141 | +You can insert IntervalDS objects into an INTERVAL DAY TO SECOND column by |
| 142 | +binding as ``oracledb.DB_TYPE_DS``, for example: |
| 143 | +
|
| 144 | +.. code-block:: javascript |
| 145 | +
|
| 146 | + await connection.execute( |
| 147 | + `INSERT INTO TableIntervalDS VALUES (:bv)`, |
| 148 | + { bv: { val: data, type: oracledb.DB_TYPE_DS } |
| 149 | + ); |
| 150 | +
|
| 151 | +.. _fetchintervaldaytosecond: |
| 152 | +
|
| 153 | +Fetching INTERVAL DAY TO SECOND |
| 154 | +------------------------------- |
| 155 | +
|
| 156 | +To query an INTERVAL DAY TO SECOND column, you can use: |
| 157 | +
|
| 158 | +.. code-block:: javascript |
| 159 | +
|
| 160 | + const result = await connection.execute(`SELECT * FROM TableIntervalDS`); |
| 161 | + console.log(result.rows[0][0]); |
| 162 | +
|
| 163 | +This query prints:: |
| 164 | +
|
| 165 | + IntervalDS { days: 2, hours: 0, minutes: 0, seconds: 40, fseconds: 0 } |
0 commit comments