Skip to main content
Skip to main content

Comparison functions

Comparison rules

The comparison functions below return 0 or 1 with type UInt8. Only values within the same group can be compared (e.g. UInt16 and UInt64) but not across groups (e.g. UInt16 and DateTime). Comparison of numbers and strings are possible, as is comparison of strings with dates and dates with times. For tuples and arrays, the comparison is lexicographic meaning that the comparison is made for each corresponding element of the left side and right side tuple/array.

The following types can be compared:

  • numbers and decimals
  • strings and fixed strings
  • dates
  • dates with times
  • tuples (lexicographic comparison)
  • arrays (lexicographic comparison)
Note

Strings are compared byte-by-byte. This may lead to unexpected results if one of the strings contains UTF-8 encoded multi-byte characters. A string S1 which has another string S2 as prefix is considered longer than S2.

equals

Introduced in: v1.1

Compares two values for equality.

Syntax

equals(a, b)
        -- a = b
        -- a == b

Arguments

  • a — First value.* - b — Second value.*

Returned value

Returns 1 if a is equal to b, otherwise 0 UInt8

Examples

Usage example

SELECT 1 = 1, 1 = 2;
┌─equals(1, 1)─┬─equals(1, 2)─┐
│            1 │            0 │
└──────────────┴──────────────┘

greater

Introduced in: v1.1

Compares two values for greater-than relation.

Syntax

greater(a, b)
    -- a > b

Arguments

  • a — First value.* - b — Second value.*

Returned value

Returns 1 if a is greater than b, otherwise 0 UInt8

Examples

Usage example

SELECT 2 > 1, 1 > 2;
┌─greater(2, 1)─┬─greater(1, 2)─┐
│             1 │             0 │
└───────────────┴───────────────┘

greaterOrEquals

Introduced in: v1.1

Compares two values for greater-than-or-equal-to relation.

Syntax

greaterOrEquals(a, b)
    -- a >= b

Arguments

  • a — First value.* - b — Second value.*

Returned value

Returns 1 if a is greater than or equal to b, otherwise 0 UInt8

Examples

Usage example

SELECT 2 >= 1, 2 >= 2, 1 >= 2;
┌─greaterOrEquals(2, 1)─┬─greaterOrEquals(2, 2)─┬─greaterOrEquals(1, 2)─┐
│                     1 │                     1 │                     0 │
└───────────────────────┴───────────────────────┴───────────────────────┘

less

Introduced in: v1.1

Compares two values for less-than relation.

Syntax

less(a, b)
    -- a < b

Arguments

  • a — First value.* - b — Second value.*

Returned value

Returns 1 if a is less than b, otherwise 0 UInt8

Examples

Usage example

SELECT 1 < 2, 2 < 1;
┌─less(1, 2)─┬─less(2, 1)─┐
│          1 │          0 │
└────────────┴────────────┘

lessOrEquals

Introduced in: v1.1

Compares two values for less-than-or-equal-to relation.

Syntax

lessOrEquals(a, b)
    -- a <= b

Arguments

  • a — First value.* - b — Second value.*

Returned value

Returns 1 if a is less than or equal to b, otherwise 0 UInt8

Examples

Usage example

SELECT 1 <= 2, 2 <= 2, 3 <= 2;
┌─lessOrEquals(1, 2)─┬─lessOrEquals(2, 2)─┬─lessOrEquals(3, 2)─┐
│                  1 │                  1 │                  0 │
└────────────────────┴────────────────────┴────────────────────┘

notEquals

Introduced in: v1.1

Compares two values for inequality.

Syntax

notEquals(a, b)
    -- a != b
    -- a <> b

Arguments

  • a — First value.* - b — Second value.*

Returned value

Returns 1 if a is not equal to b, otherwise 0. UInt8

Examples

Usage example

SELECT 1 != 2, 1 != 1;
┌─notEquals(1, 2)─┬─notEquals(1, 1)─┐
│               1 │               0 │
└─────────────────┴─────────────────┘