# Date, Timestamp, and Interval Operators

Summary: (The arithmetic operators +, -, *, and / are binary operators.)

Operator Description Notes
+ Addition interval + interval = interval
interval + datetime = datetime
datetime + interval = datetime
datetime - interval = datetime
( - ) - = interval
numeric * interval = interval

#### Examples:

| Example Number | Operation | Result | — | — | — | | — | — | — | — | | — | — | | — | — | — | | — | — | | 1st | INTERVAL ‘1’ DAY + INTERVAL ‘3’ DAY | INTERVAL ‘4’ DAY| | 3rd | INTERVAL ‘1’ DAY - INTERVAL ‘3 4’ DAY TO HOUR | INTERVAL ‘-2 04’ DAY TO HOUR| | 5th | 2 * INTERVAL ‘3 4’ DAY TO HOUR | INTERVAL ‘6 8’ DAY TO HOUR | | In the 3rd example above, ‘3 4 DAY means 3 days and 4 hours, so the result in that row means 24 hours minus 76 hours, resulting in minus 52 hours, which is a negative 2 days and 4 hours. The 4th example above uses TO MONTH rather than TO HOUR, so the INTERVAL specified as ‘3-4’ means 3 years and 4 months, or 40 months.

In the last example above, the “/2” applies to the INTERVAL ‘3 4’, which is 76 hours, half of which is 38, or 1 day and 14 hours.

## Further examples of interval operations

Streaming SQL also supports subtracting two datetimes, giving an interval; you specify what kind of interval you want for the result. ```(<datetime> - <datetime>) - <interval qualifier> ``` The following illustrative examples show interesting operations that can be useful in SQLstream s-Server applications: Time difference, as minutes to the nearest second (or as seconds):

``````values cast ((time Â '12:03:34' - time '11:57:23') minute to second as varchar(8));
+---------+
EXPR\$0 Â
+---------+
+6:11 Â
+---------+
1 row selected
............... 6 minutes, 11 seconds
or
values cast ((time Â '12:03:34' - time '11:57:23') second as varchar(8));
+---------+
EXPR\$0 Â
+---------+
+371 Â  Â
+---------+
1 row selected
``````

### Time difference, as minutes only: 2 examples

values cast ((time Â ’12:03:34’ - time ‘11:57:23’) minute as varchar(8)); +———+ EXPR\$0 Â  +———+ +6 Â  Â  Â  +———+ 1 row selected …………… 6 minutes; seconds ignored. values cast ((time Â ’12:03:23’ - time ‘11:57:23’) minute as varchar(8)); +———+ EXPR\$0 Â  +———+ +6 Â  Â  Â  +———+ 1 row selected …………… 6 minutes

``````### Time-to-timestamp difference, as days to the nearest second: Invalid example
values cast ((time '12:03:34'-timestamp '2004-04-29 11:57:23') day to second as varchar(8));
Error: From line 1, column 14 to line 1, column 79: Parameters must be of the same type
``````

### Timestamp difference, as days to the nearest second: 2 examples:

``````values cast ((timestamp Â '2004-05-01 12:03:34' - timestamp '2004-04-29 11:57:23') day to
Â  Â  Â  Â  Â  Â  Â  Â second as varchar(8));
+-----------+
Â EXPR\$0 Â
+-----------+
+2 00:06 Â
+-----------+
1 row selected
............... 2 days, 6 minutes
............... Although "second" was specified above, the varchar(8) happens to allow
only room enough to show only the minutes, not the seconds.
The example below expands to varchar(11), showing the full result:
```sql
values cast ((timestamp Â '2004-05-01 12:03:34' - timestamp '2004-04-29 11:57:23') day to
Â  Â  Â  Â  Â  Â  Â  Â second as varchar(11));
+--------------+
Â  Â EXPR\$0 Â  Â
+--------------+
+2 00:06:11 Â
+--------------+
1 row selected
............... 2 days, 6 minutes, 11 seconds
``````

### Timestamp difference, as days to the nearest second: 2 further examples

values cast ((timestamp Â ’2004-05-01 1:03:34’ - timestamp ‘2004-04-29 11:57:23’) day to Â  Â  Â  Â  Â  Â  Â  Â second as varchar(11)); +————–+ Â  Â EXPR\$0 Â  Â  +————–+ +1 13:06:11 Â  +————–+ 1 row selected …………… 1 day, 13 hours, 6 minutes, 11 seconds values cast ((timestamp Â ’2004-05-01 13:03:34’ - timestamp ‘2004-04-29 11:57:23’) day to Â  Â  Â  Â  Â  Â  Â  Â second as varchar(11)); +————–+ Â  Â EXPR\$0 Â  Â  +————–+ +2 01:06:11 Â  +————–+ 1 row selected …………… 2 days, 1 hour, 6 minutes, 11 seconds

### Timestamp difference, as days:

``````values cast ((timestamp Â '2004-05-01 12:03:34' - timestamp '2004-04-29 11:57:23') day
Â  Â  Â  Â  Â  Â  Â  Â as varchar(8));
+---------+
EXPR\$0 Â
+---------+
+2 Â  Â  Â
+---------+
1 row selected
............... 2 days
``````

### Time difference, as days; 3 examples

values cast ((date ‘2004-12-02 ’ - date ‘2003-12-01 ‘) day Â as varchar(8)); Error: Illegal DATE literal ‘2004-12-02 ‘: not in format ‘yyyy-MM-dd’ ………….. Both date literals end with a space; Â disallowed. values cast ((date ‘2004-12-02’ - date ‘2003-12-01 ‘) day Â as varchar(8)); Error: Illegal DATE literal ‘2003-12-01 ‘: not in format ‘yyyy-MM-dd’ ………….. Second date literal still ends with a space; Â disallowed. values cast ((date ‘2004-12-02’ - date ‘2003-12-01’) day Â as varchar(8)); +———+ EXPR\$0 Â  +———+ +367 Â  Â  +———+ 1 row selected …………… 367 days

``````Note that without specifying "day" as the intended unit, as shown below, the subtraction is not supported:
### Not supported: simple difference of dates:
```sql
Â  Â  values cast ((date '2004-12-02' - date '2003-12-01') as varchar(8));
Â  Â  Error: From line 1, column 15 to line 1, column 51:
```sql
Â  Â  Â  Â  Â  Â Cannot apply '-' to arguments of type '<DATE> - <DATE>'.
Â  Â  Supported form(s): '<NUMERIC> - <NUMERIC>'
Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â '<DATETIME_INTERVAL> - <DATETIME_INTERVAL>'
Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â '<DATETIME> - <DATETIME_INTERVAL>'
``````

### Why use “as varchar” in conversion examples?

The reason for using the *“values cast ( AS varchar(N))” *syntax in the examples above is that while the SQLline client used above (with the SQLstream Â s-Server running) does return an interval, JDBC does not support returning that result so as to display it. Therefore, that “values” syntax is used to see/show it. If you close the SQLstream s-Server (with a !kill command) or if you don’t start it before running SQLline, then you can run the sqllineEngine (rather than the sqllineClient) from the bin subdirectory of your SQLstream home, which can show your results without the s-Server or JDBC:

## Rules for Specifying Intervals

A Day-Time Interval Literal is a string that denotes a single interval value: for example ‘10’ SECONDS. Note it has two parts: the value (which must always be in single-quotes) and the qualifier (here, SECONDS), which give the units for the value. The qualifier takes the following form: Â  DAY Â HOUR Â MINUTE Â SECOND [TO HOUR Â MINUTE Â SECOND] Note: YEAR TO MONTH intervals require a dash separating the values, whereas DAY TO HOUR intervals use a space to separate the values, as seen in the 2nd, 3rd, 5th, and 6th examples in that topic. In addition, the leading term has to be of greater significance than the optional trailing term, so this means you can only specify: ```sql Â  DAY Â HOUR Â MINUTE Â SECOND Â DAY TO HOUR Â DAY TO MINUTE Â DAY TO SECOND Â HOUR TO MINUTE Â HOUR TO SECOND Â MINUTE TO SECOND

``````The easiest way to understand these may be to translate X TO Y as "Xs to the nearest Y". Hence, DAY TO HOUR is "days to the nearest hour".
When DAY, HOUR, or MINUTE is the leading term, you can specify a precision, e.g., DAY(3) TO HOUR, indicating the number of digits the associated field in the value can have. The maximum precision is 10, and the default is 2. You can't specify precision for HOUR, OR MINUTE in the trailing term - they are always of precision 2. So for example, HOUR(3) TO MINUTE is legal, HOUR TO MINUTE(3) is not.
SECOND can also take a precision, but the way it is specified differs depending on whether it is the leading or trailing field.
- If SECOND is the leading field, you can specify the digits before and after the decimal point. For example, SECOND(3,3) would allow you to specify up to 999.999 seconds. The default is (2,3), which is actually a deviation from the the SQL standard spec (it should be (2,6), but we only have millisecond precision).
- If SECOND is the trailing field, you can only specify precision for the fractional seconds, that is, the part shown after the seconds' decimal point below. For example, SECOND(3) would indicate milliseconds. The default is 3 digits after the decimal point, but as above this is a deviation from the standard of 6.

As for the value, it takes the general form of:
```sql
Â [+-]'[+-]DD HH:MM:SS.SSS'
``````

where DD are digits indicating days, HH hours, MM minutes, and SS.SSS is seconds (adjust the number of digits appropriately if precision is explicitly specified). Not all values have to include all fields – you can trim from both front or back, but not from in the middle. So you could make it ‘DD HH’ or ‘MM:SS.SSS’, but not ‘DD MM’. However you write it, though, the value must match the qualifier:

``````INTERVAL '25 3' DAY to HOUR ------> legal
INTERVAL '3:45:04.0' DAY TO HOUR --> illegal
``````

As stated in the SQL spec, if the precision is not explicitly specified, it is implied to be 2. Thus - *INTERVAL ‘120’ MINUTE is an illegal interval; the legal form for the desired interval is INTERVAL ‘120’ MINUTE(2)

and - *INTERVAL ‘120’ SECOND is not legal; the legal form for the desired interval is INTERVAL ‘120’ SECOND(3).

Â  values INTERVAL ‘120’ MINUTE(2); Â  Error: From line 1, column 8 to line 1, column 31:

``````Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Interval field value 120 exceeds precision of MINUTE(2) field
Â  values INTERVAL '120' MINUTE(3);
Â  Conversion not supported
``````

Also, if HOUR, MINUTE, or SECOND are not the leading field, they must fall in the following ranges (taken from Table 6 in topic 4.6.3 of the the SQL standard foundation spec):

``````Â HOUR: 0-23
Â MINUTE: 0-59
Â SECOND: 0-59.999
``````

Year-month intervals are similar, save that the qualifiers are: ```sql Â  YEAR Â MONTH Â YEAR TO MONTH ``` Precision can be specified just as with DAY and HOUR, and the max of 10 and default of 2 is the same. The value format for year-month is: ‘YY-MM’. If MONTH is the trailing field, it must fall in the range 0-11. Specifying an interval qualifier The following definitions provide the precise meaning and allowable entries in specifying an interval qualifier. := TO Â

:= [ ]

:= Â SECOND [ ]

:= [ ] Â  SECOND [ Â  Â  Â  Â  Â  [ ] ] := Â  Â  Â  SECOND := YEAR Â MONTH Â DAY Â HOUR Â  Â  Â MINUTE := :=