Arithmetic functions

Note

These functions work significantly slower than usual `multiply`.
In case you don't really need controlled precision and/or need fast computation, consider using [multiply](#multiply)

**Syntax**

```
multiplyDecimal(a, b[, result_scale])
```

**Arguments**

- `a` — First value. [`Decimal`](/docs/sql-reference/data-types/decimal)
- `b` — Second value. [`Decimal`](/docs/sql-reference/data-types/decimal)
- `result_scale` — Scale of result. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

The result of multiplication with the given scale. Type: [`Decimal256`](/docs/sql-reference/data-types/decimal)

**Examples**

**Usage example**

```
SELECT multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)
```

```
25.2
```

**Difference with regular multiplication**

```
SELECT multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)
```

```
┌─multiply(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
│                                               -26.8609633 │
└───────────────────────────────────────────────────────────┘
┌─multiplyDecimal(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐
│                                                         -26.8609 │
└──────────────────────────────────────────────────────────────────┘
```

**Decimal overflow**

```
SELECT
    toDecimal64(-12.647987876, 9) AS a,
    toDecimal64(123.967645643, 9) AS b,
    multiplyDecimal(a, b);
SELECT
    toDecimal64(-12.647987876, 9) AS a,
    toDecimal64(123.967645643, 9) AS b,
    a * b;
```

```
┌─────────────a─┬─────────────b─┬─multiplyDecimal(toDecimal64(-12.647987876, 9), toDecimal64(123.967645643, 9))─┐
│ -12.647987876 │ 123.967645643 │                                                               -1567.941279108 │
└───────────────┴───────────────┴───────────────────────────────────────────────────────────────────────────────┘
Received exception from server (version 22.11.1):
Code: 407. DB::Exception: Received from localhost:9000. DB::Exception: Decimal math overflow:
While processing toDecimal64(-12.647987876, 9) AS a, toDecimal64(123.967645643, 9) AS b, a * b. (DECIMAL_OVERFLOW)
```

## negate[​](#negate "Direct link to negate")

Introduced in: v1.1.0

Negates the argument `x`. The result is always signed.

**Syntax**

```
negate(x)
```

**Arguments**

- `x` — The value to negate.

**Returned value**

Returns -x from x

**Examples**

**Usage example**

```
SELECT negate(10)
```

```
-10
```

## plus[​](#plus "Direct link to plus")

Introduced in: v1.1.0

Calculates the sum of two values `x` and `y`. Alias: `x + y` (operator).
It is possible to add an integer and a date or date with time. The former
operation increments the number of days in the date, the latter operation
increments the number of seconds in the date with time.

**Syntax**

```
plus(x, y)
```

**Arguments**

- `x` — Left hand operand. - `y` — Right hand operand.

**Returned value**

Returns the sum of x and y

**Examples**

**Adding two numbers**

```
SELECT plus(5,5)
```

```
10
```

**Adding an integer and a date**

```
SELECT plus(toDate('2025-01-01'),5)
```

```
2025-01-06
```

## positiveModulo[​](#positiveModulo "Direct link to positiveModulo")

Introduced in: v22.11.0

Calculates the remainder when dividing `x` by `y`. Similar to function
`modulo` except that `positiveModulo` always return non-negative number.

**Syntax**

```
positiveModulo(x, y)
```

**Aliases**: `positive_modulo`, `pmod`

**Arguments**

- `x` — The dividend. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)
- `y` — The divisor (modulus). [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Returned value**

Returns the difference between `x` and the nearest integer not greater than
`x` divisible by `y`.

**Examples**

**Usage example**

```
SELECT positiveModulo(-1, 10)
```

```
9
```

## positiveModuloOrNull[​](#positiveModuloOrNull "Direct link to positiveModuloOrNull")

Introduced in: v25.5.0

Calculates the remainder when dividing `a` by `b`. Similar to function `positiveModulo` except that `positiveModuloOrNull` will return NULL
if the right argument is 0.

**Syntax**

```
positiveModuloOrNull(x, y)
```

**Aliases**: `positive_modulo_or_null`, `pmodOrNull`

**Arguments**

- `x` — The dividend. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)/[`Float32/64`](/docs/sql-reference/data-types/float). - `x` — The divisor (modulus). [`(U)Int*`](/docs/sql-reference/data-types/int-uint)/[`Float32/64`](/docs/sql-reference/data-types/float).

**Returned value**

Returns the difference between `x` and the nearest integer not greater than
`x` divisible by `y`, `null` when the divisor is zero.

**Examples**

**positiveModuloOrNull**

```
SELECT positiveModuloOrNull(5, 0)
```

```
\N
```

- [Overview](#overview)- [abs](#abs)- [avg2](#avg2)- [byteSwap](#byteSwap)- [divide](#divide)- [divideDecimal](#divideDecimal)- [divideOrNull](#divideOrNull)- [gcd](#gcd)- [ifNotFinite](#ifNotFinite)- [intDiv](#intDiv)- [intDivOrNull](#intDivOrNull)- [intDivOrZero](#intDivOrZero)- [isFinite](#isFinite)- [isInfinite](#isInfinite)- [isNaN](#isNaN)- [lcm](#lcm)- [max2](#max2)- [midpoint](#midpoint)- [min2](#min2)- [minus](#minus)- [modulo](#modulo)- [moduloLegacy](#moduloLegacy)- [moduloOrNull](#moduloOrNull)- [moduloOrZero](#moduloOrZero)- [multiply](#multiply)- [multiplyDecimal](#multiplyDecimal)- [negate](#negate)- [plus](#plus)- [positiveModulo](#positiveModulo)- [positiveModuloOrNull](#positiveModuloOrNull)

Was this page helpful?