ClickHouse/docs/en/sql-reference/functions/logical-functions.md

191 lines
4.9 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
toc_priority: 37
toc_title: Logical
---
# Logical Functions {#logical-functions}
Performs logical operations on arguments of any numeric types, but returns a [UInt8](../../sql-reference/data-types/int-uint.md) number equal to 0, 1 or `NULL` in some cases.
Zero as an argument is considered `false`, while any non-zero value is considered `true`.
2021-06-29 19:42:34 +00:00
## and {#logical-and-function}
2021-06-28 18:44:59 +00:00
Calculates the result of the logical conjunction between two or more values. Corresponds to [Logical AND Operator](../../sql-reference/operators/index.md#logical-and-operator).
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
and(val1, val2...)
```
**Arguments**
2021-07-29 15:20:55 +00:00
- `val1, val2, ...` — List of at least two values. [Int](../../sql-reference/data-types/int-uint.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md) or [Nullable](../../sql-reference/data-types/nullable.md).
2021-06-28 18:25:52 +00:00
**Returned value**
- `0`, if there is at least one zero value argument.
- `NULL`, if there are no zero values arguments and there is at least one `NULL` argument.
- `1`, otherwise.
2021-06-28 18:44:59 +00:00
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
2021-06-28 18:25:52 +00:00
**Example**
Query:
``` sql
SELECT and(0, 1, -2);
```
Result:
``` text
┌─and(0, 1, -2)─┐
│ 0 │
└───────────────┘
```
With `NULL`:
``` sql
SELECT and(NULL, 1, 10, -2);
```
Result:
``` text
┌─and(NULL, 1, 10, -2)─┐
│ ᴺᵁᴸᴸ │
└──────────────────────┘
```
2021-06-29 19:42:34 +00:00
## or {#logical-or-function}
2021-06-28 18:25:52 +00:00
2021-06-28 18:44:59 +00:00
Calculates the result of the logical disjunction between two or more values. Corresponds to [Logical OR Operator](../../sql-reference/operators/index.md#logical-or-operator).
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
and(val1, val2...)
```
**Arguments**
2021-07-29 15:20:55 +00:00
- `val1, val2, ...` — List of at least two values. [Int](../../sql-reference/data-types/int-uint.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md) or [Nullable](../../sql-reference/data-types/nullable.md).
2021-06-28 18:25:52 +00:00
**Returned value**
- `1`, if there is at least one non-zero value.
- `0`, if there are only zero values.
- `NULL`, if there are only zero values and `NULL`.
2021-06-28 18:25:52 +00:00
2021-06-28 18:44:59 +00:00
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
2021-06-28 18:25:52 +00:00
**Example**
Query:
``` sql
SELECT or(1, 0, 0, 2, NULL);
```
Result:
``` text
┌─or(1, 0, 0, 2, NULL)─┐
│ 1 │
└──────────────────────┘
```
With `NULL`:
``` sql
SELECT or(0, NULL);
```
Result:
``` text
┌─or(0, NULL)─┐
│ ᴺᵁᴸᴸ │
└─────────────┘
```
2021-06-29 19:42:34 +00:00
## not {#logical-not-function}
2021-06-28 18:25:52 +00:00
Calculates the result of the logical negation of the value. Corresponds to [Logical Negation Operator](../../sql-reference/operators/index.md#logical-negation-operator).
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
not(val);
```
**Arguments**
2021-07-29 15:20:55 +00:00
- `val` — The value. [Int](../../sql-reference/data-types/int-uint.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md) or [Nullable](../../sql-reference/data-types/nullable.md).
2021-06-28 18:25:52 +00:00
**Returned value**
- `1`, if the `val` is `0`.
2021-06-28 18:25:52 +00:00
- `0`, if the `val` is a non-zero value.
- `NULL`, if the `val` is a `NULL` value.
2021-06-28 18:44:59 +00:00
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
2021-06-28 18:25:52 +00:00
**Example**
Query:
``` sql
SELECT NOT(1);
```
Result:
``` test
┌─not(1)─┐
│ 0 │
└────────┘
```
2018-04-23 06:20:21 +00:00
2021-06-29 19:42:34 +00:00
## xor {#logical-xor-function}
2021-07-02 10:23:00 +00:00
Calculates the result of the logical exclusive disjunction between two or more values. For more than two values the function works as if it calculates `XOR` of the first two values and then uses the result with the next value to calculate `XOR` and so on.
2021-06-28 18:25:52 +00:00
**Syntax**
``` sql
xor(val1, val2...)
```
**Arguments**
2021-07-29 15:20:55 +00:00
- `val1, val2, ...` — List of at least two values. [Int](../../sql-reference/data-types/int-uint.md), [UInt](../../sql-reference/data-types/int-uint.md), [Float](../../sql-reference/data-types/float.md) or [Nullable](../../sql-reference/data-types/nullable.md).
2021-06-28 18:25:52 +00:00
**Returned value**
2021-07-29 15:20:55 +00:00
- `1`, for two values: if one of the values is zero and other is not.
2021-06-29 20:47:19 +00:00
- `0`, for two values: if both values are zero or non-zero at the same time.
2021-06-29 19:42:34 +00:00
- `NULL`, if there is at least one `NULL` value.
2021-06-28 18:25:52 +00:00
2021-06-28 18:44:59 +00:00
Type: [UInt8](../../sql-reference/data-types/int-uint.md) or [Nullable](../../sql-reference/data-types/nullable.md)([UInt8](../../sql-reference/data-types/int-uint.md)).
2021-06-28 18:25:52 +00:00
**Example**
Query:
``` sql
SELECT xor(0, 1, 1);
```
Result:
2021-06-28 18:25:52 +00:00
``` text
┌─xor(0, 1, 1)─┐
│ 0 │
└──────────────┘
```