---
title: "Arithmetic operators"
description: "CoraDB arithmetic operators (+, -, *, /, DIV, MOD) on numbers and dates: result types, overflow and division by zero."
url: "https://www.coradb.ai/manual/sql/function/arithmetic_op.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/function/arithmetic_op.md"
product: "CoraDB"
version: "1.0"
keywords: "arithmetic operators, addition, subtraction, multiplication, division, div, mod, numeric type conversion, date arithmetic, coradb"
---

# Arithmetic operators

For arithmetic operators, there are binary operators for addition, subtraction, multiplication, or division, and unary operators to represent whether the number is positive or negative. The unary operators to represent the numbers' positive/negative status have higher priority over the binary operators.

```text
<expression>  <mathematical_operator>  <expression>

    <expression> ::=
        bit_string |
        character_string |
        numeric_value |
        date-time_value |
        collection_value |
        NULL

    <mathematical_operator> ::=
        <set_arithmetic_operator> |
        <arithmetic_operator>

            <arithmetic_operator> ::=
                + |
                - |
                * |
                { / | DIV } |
                { % | MOD }

            <set_arithmetic_operator> ::=
                UNION |
                DIFFERENCE |
                { INTERSECT | INTERSECTION }
```

- \<*expression*\>: Declares the mathematical operation to be calculated.
- \<*mathematical_operator*\>: A operator that performs an operation the arithmetic and the set operators are applicable.
  - \<*set_arithmetic_operator*\>: A set arithmetic operator that performs operations such as union, difference and intersection on collection type operands.
  - \<*arithmetic_operator*\>: An operator to perform the four fundamental arithmetic operations.

The following table shows the arithmetic operators supported by CoraDB and their return values.

**Arithmetic operators**

| Arithmetic operator \| Description |     |                                                                                                                                                                 | Operator      | Return value |
|------------------------------------|-----|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|--------------|
| **+** \| Addition                  |     |                                                                                                                                                                 | 1+2           | 3            |
| **-**                              |     | Subtraction                                                                                                                                                     | 1-2           | -1           |
| **\***                             |     | Multiplication                                                                                                                                                  | 1\*2          | 2            |
| **/**                              |     | Division. Returns quotient.                                                                                                                                     | 1/2.0         | 0.500000000  |
| **DIV**                            |     | Division. Returns quotient.                                                                                                                                     | 1 DIV 2       | 0            |
| **%** , **MOD**                    |     | Division. Returns quotient. An operator must be an integer type, and it always returns integer. If an operand is real number, the **MOD** function can be used. | 1 % 2 1 MOD 2 | 1            |

## Arithmetic operations and type casting of numeric data types

All numeric data types can be used for arithmetic operations. The result type of the operation differs depending on the data types of the operands and the type of the operation. The following table shows the result data types of addition/subtraction/multiplication for each operand type.

**Result data type by operand type**

|             | INT           | NUMERIC                              | FLOAT  | DOUBLE |
|-------------|---------------|--------------------------------------|--------|--------|
| **INT**     | INT or BIGINT | NUMERIC                              | FLOAT  | DOUBLE |
| **NUMERIC** | NUMERIC       | NUMERIC (p and s are also converted) | DOUBLE | DOUBLE |
| **FLOAT**   | FLOAT         | DOUBLE                               | FLOAT  | DOUBLE |
| **DOUBLE**  | DOUBLE        | DOUBLE                               | DOUBLE | DOUBLE |

Note that the result type of the operation does not change if all operands are of the same data type but type casting occurs exceptionally in division operations. An error occurs when a denominator, i.e. a divisor, is 0.

The following table shows the total number of digits (*p*) and the number of digits after the decimal point (*s*) of the operation results when all operands are of the **NUMERIC** type.

**Result of NUMERIC type operation**

| Operation              | Maximum precision                                                                                                                                                                                                       | Maximum scale |
|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|
| N(p1, s1) + N(p2, s2)  | max(p1-s1, p2-s2)+max(s1, s2) +1                                                                                                                                                                                        | Max(s1, s2)   |
| N(p1, s1) - N(p2, s2)  | max(p1-s1, p2-s2)+max(s1, s2)                                                                                                                                                                                           | Max(s1, s2)   |
| N(p1, s1) \* N(p2, s2) | p1+p2+1                                                                                                                                                                                                                 | s1+s2         |
| N(p1, s1) / N(p2, s2)  | Let Pt = p1+max(s1, s2) + s2 - s1 when s2 \> 0 and Pt = p1 in other cases; St = s1 when s1 \> s2 and s2 in other cases; \| The number of decimal places is min(9-St, 38-Pt) + St when St \< 9 and St in other cases. \| |               |

**Example**

```sql
--int * int
SELECT 123*123;
```

| 123*123 |
|---|
| 15129 |

```sql
-- int * int returns overflow error
SELECT (1234567890123*1234567890123);
```

```text
ERROR: Data overflow on data type bigint.
```

```sql
-- int * numeric returns numeric type  
SELECT (1234567890123*CAST(1234567890123 AS NUMERIC(15,2)));
```

| (1234567890123*CAST(1234567890123 AS NUMERIC(15,2))) |
|---|
| 1524157875322755800955129.00 |

```sql
-- int * float returns float type
SELECT (1234567890123*CAST(1234567890123 AS FLOAT));
```

| (1234567890123*CAST(1234567890123 AS FLOAT)) |
|---|
| 1.524158e+24 |

```sql
-- int * double returns double type
SELECT (1234567890123*CAST(1234567890123 AS DOUBLE));
```

| (1234567890123*CAST(1234567890123 AS DOUBLE)) |
|---|
| 1.524157875322755e+24 |

```sql
-- numeric * numeric returns numeric type   
SELECT (CAST(1234567890123 AS NUMERIC(15,2))*CAST(1234567890123 AS NUMERIC(15,2)));
```

| (CAST(1234567890123 AS NUMERIC(15,2))*CAST(1234567890123 AS NUMERIC(15,2))) |
|---|
| 1524157875322755800955129.0000 |

```sql
-- numeric * float returns double type  
SELECT (CAST(1234567890123 AS NUMERIC(15,2))*CAST(1234567890123 AS FLOAT));
```

| (CAST(1234567890123 AS NUMERIC(15,2))*CAST(1234567890123 AS FLOAT)) |
|---|
| 1.524157954716582e+24 |

```sql
-- numeric * double returns double type  
SELECT (CAST(1234567890123 AS NUMERIC(15,2))*CAST(1234567890123 AS DOUBLE));
```

| (CAST(1234567890123 AS NUMERIC(15,2))*CAST(1234567890123 AS DOUBLE)) |
|---|
| 1.524157875322755e+24 |

```sql
-- float * float returns float type  
SELECT (CAST(1234567890123 AS FLOAT)*CAST(1234567890123 AS FLOAT));
```

| (CAST(1234567890123 AS FLOAT)*CAST(1234567890123 AS FLOAT)) |
|---|
| 1.524158e+24 |

```sql
-- float * double returns float type  
SELECT (CAST(1234567890123 AS FLOAT)*CAST(1234567890123 AS DOUBLE));
```

| (CAST(1234567890123 AS FLOAT)*CAST(1234567890123 AS DOUBLE)) |
|---|
| 1.524157954716582e+24 |

```sql
-- double * double returns float type  
SELECT (CAST(1234567890123 AS DOUBLE)*CAST(1234567890123 AS DOUBLE));
```

| (CAST(1234567890123 AS DOUBLE)*CAST(1234567890123 AS DOUBLE)) |
|---|
| 1.524157875322755e+24 |

```sql
-- int / int returns int type without type conversion or rounding
SELECT 100100/100000;
```

| 100100/100000 |
|---|
| 1 |

```sql
-- int / int returns int type without type conversion or rounding
SELECT 100100/200200;
```

| 100100/200200 |
|---|
| 0 |

```sql
-- int / zero returns error
SELECT 100100/(100100-100100);
```

```text
ERROR: Attempt to divide by zero.
```

## Arithmetic operations and type casting of DATE/TIME data types

If all operands are date/time type, only a subtraction operation is allowed and its return value is **BIGINT** . Note that the unit of the operation differs depending on the types of the operands. Both addition and subtraction operations are allowed in case of date/time and integer types In this case, operation units and return values are date/time data type.

The following table shows operations allowed for each operand type, and their result types.

**Allowable operation and result data type by operand type**

|               | **TIME** **(in seconds)**                           | **DATE** **(in day)**                               | **TIMESTAMP** **(in seconds)**                           | **DATETIME** **(in milliseconds)**                      | **INT**                                                  |
|---------------|-----------------------------------------------------|-----------------------------------------------------|----------------------------------------------------------|---------------------------------------------------------|----------------------------------------------------------|
| **TIME**      | A subtraction is allowed. **BIGINT**                | X                                                   | X                                                        | X                                                       | An addition and a subtraction are allowed. **TIME**      |
| **DATE**      | X                                                   | A subtraction is allowed. **BIGINT**                | A subtraction is allowed. **BIGINT**                     | A subtraction is allowed. **BIGINT**                    | An addition and a subtraction are allowed. **DATE**      |
| **TIMESTAMP** | X                                                   | A subtraction is allowed. **BIGINT**                | A subtraction is allowed. **BIGINT**                     | A subtraction is allowed. **BIGINT**                    | An addition and a subtraction are allowed. **TIMESTAMP** |
| **DATETIME**  | X                                                   | A subtraction is allowed. **BIGINT**                | A subtraction is allowed. **BIGINT**                     | A subtraction is allowed. **BIGINT**                    | An addition and a subtraction are allowed. **DATETIME**  |
| **INT**       | An addition and a subtraction are allowed. **TIME** | An addition and a subtraction are allowed. **DATE** | An addition and a subtraction are allowed. **TIMESTAMP** | An addition and a subtraction are allowed. **DATETIME** | All operations are allowed.                              |

> **Note:**
> If any of the date/time arguments contains **NULL**, **NULL** is returned.


**Example**

Adding a number to a date/time value adds days to a `DATE`, seconds to a `TIME` and `TIMESTAMP`, and milliseconds to a `DATETIME`:

```sql
SELECT TIME'23:59:55' + 10, DATE'2025-12-25' + 10,
       TIMESTAMP'2025-12-31 23:59:55' + 10, DATETIME'2025-12-31 23:59:59.995' + 10;
```

| TIME'23:59:55' + 10 | DATE'2025-12-25' + 10 | TIMESTAMP'2025-12-31 23:59:55' + 10 | DATETIME'2025-12-31 23:59:59.995' + 10 |
|---|---|---|---|
| 00:00:05 | 2026-01-04 | 2026-01-01 00:00:05 | 2026-01-01 00:00:00.005 |

```sql
SELECT DATETIME '09/01/2009 03:30:30.001 pm'- TIMESTAMP '08/31/2009 03:30:30 pm';
```

| DATETIME '09/01/2009 03:30:30.001 pm'- TIMESTAMP '08/31/2009 03:30:30 pm' |
|---|
| 86400001 |

```sql
SELECT TIMESTAMP '09/01/2009 03:30:30 pm'- TIMESTAMP '08/31/2009 03:30:30 pm';
```

| TIMESTAMP '09/01/2009 03:30:30 pm'- TIMESTAMP '08/31/2009 03:30:30 pm' |
|---|
| 86400 |

### Behavior related to timezone parameters

TIMESTAMP and TIMESTAMP WITH LOCAL TIME ZONE data types stores internally UNIX epoch values (number of seconds elapsed from 1970). When leap second is used (tz_leap_second_support is set to yes, see [Timezone parameter](https://www.coradb.ai/manual/admin/config_sql.html#timezone-parameter)), they may contain virtual date-time values.

```sql
Virtual date-time       Unix timestamp
2008-12-31 23:59:58  -> 79399951
2008-12-31 23:59:59  -> 79399952
2008-12-31 23:59:60  -> 79399953    -> not real date (introduced by leap second)
2009-01-01 00:00:00  -> 79399954
2009-01-01 00:00:01  -> 79399955
```

Arithmetic operations with TIMESTAMP and TIMESTAMPLTZ values are performed directly on Unix epoch values, and epoch values that correspond to non-existing date/time values are allowed. With leap-second support enabled, the following comparison is therefore false, because it compares the Unix timestamps 79399952 and 79399953:

```sql
SELECT TIMESTAMPLTZ'2008-12-31 23:59:59 UTC' = TIMESTAMPLTZ'2008-12-31 23:59:59 UTC' + 1;
```

When the same values are TIMESTAMPTZ, the result of the addition is adjusted to a valid date-time, so the two values are equal. Adding one second to `TIMESTAMPLTZ'2008-12-31 23:59:59 UTC'` is displayed as `11:59:59 PM 12/31/2008`, the closest real time, although its epoch value is one second later.

Since '2008-12-31 23:59:60 UTC' corresponding to Unix timestamp value 79399953 is not a real date, the immediately preceding value is used. Internally, it is equivalent to the value ('2008-12-31 23:59:60 UTC').

TIMESTAMP WITH TIME ZONE data type contains both a UNIX timestamp and a timezone identifier. Arithmetic on TIMESTAMPTZ is also performed on UNIX timestamp part value, but is followed by an automatic adjusting operation. The presence of timezone identifier (which includes region, offset and daylight saving), requires the TIMESTAMPTZ object to be a valid date-time. The operation timestamptz'2008-12-31 23:59:59 UTC'+1 implies an automatic validation-conversion: instead of (79399953, UTC) which is not a valid date-time the value is automatically converted to (79399952,UTC) which corresponds to '2008-12-31 23:59:59 UTC'.

After each arithmetic operation implying DATETIMETZ and TIMESTAMPTZ, CoraDB performs an automatic adjustment of result value which involves:  
- Adjusting the timezone identifier : adding a number of seconds to a date with timezone may lead to change of internally stored offset rule, daylight saving rule, hence the timezone identifier must be updated
- Adjusting the Unix timestamp (only for TIMESTAMPTZ): virtual date-time values (when leap-second is enabled) are always converted to the immediately preceding Unix timestamp value.
