2017-12-28 15:13:23 +00:00
# Operators
2019-12-06 12:22:55 +00:00
All operators are transformed to their corresponding functions at the query parsing stage in accordance with their precedence and associativity.
2017-12-28 15:13:23 +00:00
Groups of operators are listed in order of priority (the higher it is in the list, the earlier the operator is connected to its arguments).
2018-08-10 14:44:49 +00:00
## Access Operators
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a[N]` – Access to an element of an array. The `arrayElement(a, N)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a.N` – Access to a tuple element. The `tupleElement(a, N)` function.
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Numeric Negation Operator
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`-a` – The `negate (a)` function.
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Multiplication and Division Operators
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a * b` – The `multiply (a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a / b` – The `divide(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a % b` – The `modulo(a, b)` function.
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Addition and Subtraction Operators
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a + b` – The `plus(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a - b` – The `minus(a, b)` function.
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Comparison Operators
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a = b` – The `equals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a == b` – The `equals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a != b` – The `notEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a <> b` – The `notEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a <= b` – The `lessOrEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a >= b` – The `greaterOrEquals(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a < b` – The `less(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a > b` – The `greater(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a LIKE s` – The `like(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a NOT LIKE s` – The `notLike(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a BETWEEN b AND c` – The same as `a >= b AND a <= c` .
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a NOT BETWEEN b AND c` – The same as `a < b OR a > c` .
2019-03-26 12:44:25 +00:00
2018-08-10 14:44:49 +00:00
## Operators for Working With Data Sets
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
*See [IN operators ](select.md#select-in-operators ).*
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a IN ...` – The `in(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a NOT IN ...` – The `notIn(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a GLOBAL IN ...` – The `globalIn(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a GLOBAL NOT IN ...` – The `globalNotIn(a, b)` function.
2017-12-28 15:13:23 +00:00
2019-11-06 12:14:41 +00:00
## Operators for Working with Dates and Times {#operators-datetime}
### EXTRACT {#operator-extract}
2019-04-10 18:58:42 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-04-10 18:58:42 +00:00
EXTRACT(part FROM date);
```
Extracts a part from a given date. For example, you can retrieve a month from a given date, or a second from a time.
The `part` parameter specifies which part of the date to retrieve. The following values are available:
- `DAY` — The day of the month. Possible values: 1– 31.
- `MONTH` — The number of a month. Possible values: 1– 12.
- `YEAR` — The year.
- `SECOND` — The second. Possible values: 0– 59.
- `MINUTE` — The minute. Possible values: 0– 59.
- `HOUR` — The hour. Possible values: 0– 23.
The `part` parameter is case-insensitive.
The `date` parameter specifies the date or the time to process. Either [Date ](../data_types/date.md ) or [DateTime ](../data_types/datetime.md ) type is supported.
Examples:
2019-09-23 15:31:46 +00:00
```sql
2019-04-10 18:58:42 +00:00
SELECT EXTRACT(DAY FROM toDate('2017-06-15'));
SELECT EXTRACT(MONTH FROM toDate('2017-06-15'));
SELECT EXTRACT(YEAR FROM toDate('2017-06-15'));
```
2019-07-12 12:03:33 +00:00
In the following example we create a table and insert into it a value with the `DateTime` type.
2019-04-10 18:58:42 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-04-10 18:58:42 +00:00
CREATE TABLE test.Orders
(
2019-07-12 12:03:33 +00:00
OrderId UInt64,
OrderName String,
2019-04-10 18:58:42 +00:00
OrderDate DateTime
)
ENGINE = Log;
```
2019-09-23 15:31:46 +00:00
```sql
2019-04-10 18:58:42 +00:00
INSERT INTO test.Orders VALUES (1, 'Jarlsberg Cheese', toDateTime('2008-10-11 13:23:44'));
```
2019-09-23 15:31:46 +00:00
```sql
2019-07-12 12:03:33 +00:00
SELECT
toYear(OrderDate) AS OrderYear,
toMonth(OrderDate) AS OrderMonth,
toDayOfMonth(OrderDate) AS OrderDay,
toHour(OrderDate) AS OrderHour,
2019-04-10 18:58:42 +00:00
toMinute(OrderDate) AS OrderMinute,
toSecond(OrderDate) AS OrderSecond
FROM test.Orders;
2019-09-23 15:31:46 +00:00
```
```text
2019-04-10 18:58:42 +00:00
┌─OrderYear─┬─OrderMonth─┬─OrderDay─┬─OrderHour─┬─OrderMinute─┬─OrderSecond─┐
│ 2008 │ 10 │ 11 │ 13 │ 23 │ 44 │
└───────────┴────────────┴──────────┴───────────┴─────────────┴─────────────┘
```
2019-09-23 16:18:19 +00:00
You can see more examples in [tests ](https://github.com/ClickHouse/ClickHouse/blob/master/dbms/tests/queries/0_stateless/00619_extract.sql ).
2019-04-10 18:58:42 +00:00
2019-11-06 12:14:41 +00:00
### INTERVAL {#operator-interval}
Creates an [Interval ](../data_types/special_data_types/interval.md )-type value that should be used in arithmetical operations with [Date ](../data_types/date.md ) and [DateTime ](../data_types/datetime.md )-type values.
Types of intervals:
- `SECOND`
- `MINUTE`
- `HOUR`
- `DAY`
- `WEEK`
- `MONTH`
- `QUARTER`
- `YEAR`
!!! warning "Warning"
2019-12-06 12:22:55 +00:00
Intervals with different types can't be combined. You can't use expressions like `INTERVAL 4 DAY 1 HOUR` . Express intervals in units that are smaller or equal the the smallest unit of the interval, for example `INTERVAL 25 HOUR` . You can use consequtive operations like in the example below.
2019-11-06 12:14:41 +00:00
Example:
```sql
SELECT now() AS current_date_time, current_date_time + INTERVAL 4 DAY + INTERVAL 3 HOUR
```
```text
┌───current_date_time─┬─plus(plus(now(), toIntervalDay(4)), toIntervalHour(3))─┐
│ 2019-10-23 11:16:28 │ 2019-10-27 14:16:28 │
└─────────────────────┴────────────────────────────────────────────────────────┘
```
**See Also**
- [Interval ](../data_types/special_data_types/interval.md ) data type
- [toInterval ](functions/type_conversion_functions.md#function-tointerval ) type convertion functions
2018-08-10 14:44:49 +00:00
## Logical Negation Operator
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`NOT a` – The `not(a)` function.
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Logical AND Operator
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a AND b` – The`and(a, b)` function.
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Logical OR Operator
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a OR b` – The `or(a, b)` function.
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Conditional Operator
2017-12-28 15:13:23 +00:00
2019-12-06 12:22:55 +00:00
`a ? b : c` – The `if(a, b, c)` function.
2017-12-28 15:13:23 +00:00
Note:
2018-09-04 11:18:59 +00:00
The conditional operator calculates the values of b and c, then checks whether condition a is met, and then returns the corresponding value. If `b` or `C` is an [arrayJoin() ](functions/array_join.md#functions_arrayjoin ) function, each row will be replicated regardless of the "a" condition.
2018-12-21 19:23:55 +00:00
## Conditional Expression {#operator_case}
2017-12-28 15:13:23 +00:00
2019-09-23 15:31:46 +00:00
```sql
2017-12-28 15:13:23 +00:00
CASE [x]
WHEN a THEN b
[WHEN ... THEN ...]
2018-11-01 13:28:45 +00:00
[ELSE c]
2017-12-28 15:13:23 +00:00
END
```
2018-11-01 13:28:45 +00:00
If `x` is specified, then `transform(x, [a, ...], [b, ...], c)` function is used. Otherwise – `multiIf(a, b, ..., c)` .
If there is no `ELSE c` clause in the expression, the default value is `NULL` .
The `transform` function does not work with `NULL` .
2017-12-28 15:13:23 +00:00
2018-08-10 14:44:49 +00:00
## Concatenation Operator
2017-12-28 15:13:23 +00:00
`s1 || s2` – The `concat(s1, s2) function.`
2018-08-10 14:44:49 +00:00
## Lambda Creation Operator
2017-12-28 15:13:23 +00:00
`x -> expr` – The `lambda(x, expr) function.`
The following operators do not have a priority, since they are brackets:
2018-08-10 14:44:49 +00:00
## Array Creation Operator
2017-12-28 15:13:23 +00:00
`[x1, ...]` – The `array(x1, ...) function.`
2018-08-10 14:44:49 +00:00
## Tuple Creation Operator
2017-12-28 15:13:23 +00:00
`(x1, x2, ...)` – The `tuple(x2, x2, ...) function.`
## Associativity
All binary operators have left associativity. For example, `1 + 2 + 3` is transformed to `plus(plus(1, 2), 3)` .
2018-12-25 15:25:43 +00:00
Sometimes this doesn't work the way you expect. For example, ` SELECT 4 > 2 > 3` will result in 0.
2017-12-28 15:13:23 +00:00
For efficiency, the `and` and `or` functions accept any number of arguments. The corresponding chains of `AND` and `OR` operators are transformed to a single call of these functions.
2018-09-04 11:18:59 +00:00
## Checking for `NULL`
ClickHouse supports the `IS NULL` and `IS NOT NULL` operators.
2018-12-21 19:23:55 +00:00
### IS NULL {#operator-is-null}
2018-09-04 11:18:59 +00:00
2018-12-12 17:28:00 +00:00
- For [Nullable ](../data_types/nullable.md ) type values, the `IS NULL` operator returns:
2018-09-04 11:18:59 +00:00
- `1` , if the value is `NULL` .
- `0` otherwise.
- For other values, the `IS NULL` operator always returns `0` .
2019-09-23 15:31:46 +00:00
```sql
SELECT x+100 FROM t_null WHERE y IS NULL
```
```text
2018-09-04 11:18:59 +00:00
┌─plus(x, 100)─┐
│ 101 │
└──────────────┘
```
### IS NOT NULL
2018-12-12 17:28:00 +00:00
- For [Nullable ](../data_types/nullable.md ) type values, the `IS NOT NULL` operator returns:
2018-09-04 11:18:59 +00:00
- `0` , if the value is `NULL` .
- `1` otherwise.
- For other values, the `IS NOT NULL` operator always returns `1` .
2019-09-23 15:31:46 +00:00
```sql
SELECT * FROM t_null WHERE y IS NOT NULL
```
```text
2018-09-04 11:18:59 +00:00
┌─x─┬─y─┐
│ 2 │ 3 │
└───┴───┘
```
2018-10-16 10:47:17 +00:00
2020-01-30 10:34:55 +00:00
[Original article ](https://clickhouse.tech/docs/en/query_language/operators/ ) <!--hide-->