- [subtraction]

All functions > MATH > - [subtraction]

Subtracts the right-hand value from the left-hand value (numeric, temporal, or interval forms).

Syntax

left - right  ·  SUBTRACT(left, right)

Function form

Numeric subtraction: Result of the subtraction

SUBTRACT(number1: T, number2: T) → T
sql
ParameterTypeRequiredDescription
number1TYesNumber to subtract from
number2TYesNumber to subtract

With:

  • T : Any numeric type

  • Both arguments must be of the same numeric type

  • Returns NULL if either operand is NULL

  • Order matters: SUBTRACT(a, b) ≠ SUBTRACT(b, a)

Date and time subtraction: Timestamp after subtracting an interval, or the interval between two datetimes

SUBTRACT(timestamp1: TIMESTAMP or DATE, timestamp2: INTERVAL, TIMESTAMP, or DATE) → TIMESTAMP or INTERVAL
sql
ParameterTypeRequiredDescription
timestamp1TIMESTAMP or DATEYesTimestamp or date to subtract from
timestamp2INTERVAL, TIMESTAMP, or DATEYesInterval to subtract, or another timestamp/date whose difference is taken
  • timestamp - interval or date - interval → TIMESTAMP
  • timestamp1 - timestamp2 or date1 - date2 → INTERVAL
  • Returns NULL if either operand is NULL

Notes

  • Numeric subtraction: same-type operands; returns NULL if either operand is NULL
  • timestamp - interval / date - interval yield a TIMESTAMP
  • timestamp1 - timestamp2 / date1 - date2 yield an INTERVAL
  • Order matters: a - b is not the same as b - a

Examples

- — numeric

FeatureQL
RETURN
    -- Basic subtraction
    f1 := 10 - 3,
    -- Subtracting a negative
    f2 := 10 - (-3)
;
Result
f1 BIGINTf2 BIGINT
713

- — date and time

FeatureQL
RETURN
    -- Timestamp minus interval
    f1 := TIMESTAMP '2021-01-02' - INTERVAL '1 DAY',
    -- Difference of two timestamps
    f2 := TIMESTAMP '2021-01-02' - TIMESTAMP '2021-01-01'
;
Result
f1 TIMESTAMPf2 VARCHAR
2021-01-01T00:00:00P1D

- — floating-point

FeatureQL
RETURN
    -- DOUBLE operands
    f1 := 2.5e0 - 1.0e0
;
Result
f1 DECIMAL
1.5

SUBTRACT(...)

FeatureQL
RETURN
    -- Function call
    f1 := SUBTRACT(10, 3)
;
Result
f1 BIGINT
7

.SUBTRACT(...) — chained

FeatureQL
RETURN
    -- Integer literal: use parentheses for `.SUBTRACT` on a BIGINT value
    f1 := (10).SUBTRACT(3),
    -- Decimal base: chain without extra parentheses
    f2 := 10.0.SUBTRACT(3)
;
Result
f1 BIGINTf2 DECIMAL
77.0

SUBTRACT — Date and time

FeatureQL
RETURN
    -- Chained subtract on a timestamp
    f1 := TIMESTAMP '2021-01-02'.SUBTRACT(INTERVAL '1 DAY'),
    -- Interval between two timestamps via chained call
    f2 := TIMESTAMP '2021-01-02'.SUBTRACT(TIMESTAMP '2021-01-01')
;
Result
f1 TIMESTAMPf2 VARCHAR
2021-01-01T00:00:00P1D

SUBTRACT — Edge cases

FeatureQL
RETURN
    -- Equal operands
    f1 := SUBTRACT(5, 5),
    -- Negative result
    f2 := SUBTRACT(1, 7),
    -- Subtracting a negative
    f3 := SUBTRACT(0, -5)
;
Result
f1 BIGINTf2 BIGINTf3 BIGINT
0-65