- [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
| Parameter | Type | Required | Description |
|---|---|---|---|
number1 | T | Yes | Number to subtract from |
number2 | T | Yes | Number to subtract |
With:
T: Any numeric typeBoth 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
| Parameter | Type | Required | Description |
|---|---|---|---|
timestamp1 | TIMESTAMP or DATE | Yes | Timestamp or date to subtract from |
timestamp2 | INTERVAL, TIMESTAMP, or DATE | Yes | Interval to subtract, or another timestamp/date whose difference is taken |
timestamp - intervalordate - interval→ TIMESTAMPtimestamp1 - timestamp2ordate1 - 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 - intervalyield a TIMESTAMPtimestamp1 - timestamp2/date1 - date2yield an INTERVAL- Order matters:
a - bis not the same asb - a
Examples
- — numeric
FeatureQL
RETURN
-- Basic subtraction
f1 := 10 - 3,
-- Subtracting a negative
f2 := 10 - (-3)
;Result
| f1 BIGINT | f2 BIGINT |
|---|---|
| 7 | 13 |
- — 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 TIMESTAMP | f2 VARCHAR |
|---|---|
| 2021-01-01T00:00:00 | P1D |
- — 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 BIGINT | f2 DECIMAL |
|---|---|
| 7 | 7.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 TIMESTAMP | f2 VARCHAR |
|---|---|
| 2021-01-01T00:00:00 | P1D |
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 BIGINT | f2 BIGINT | f3 BIGINT |
|---|---|---|
| 0 | -6 | 5 |