Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/en/sql-reference/data-types/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ SELECT * FROM tab ORDER BY event_id;
**2.** Filtering on `Time` values

``` sql
SET use_legacy_to_time = 0;
SELECT * FROM tab WHERE time = toTime('14:30:25')
```

Expand Down Expand Up @@ -115,6 +116,23 @@ SELECT CAST('14:30:25' AS Time) AS column, toTypeName(column) AS type
└───────────┴──────┘
```

## Addition with Date {#addition-with-date}

A [Time](time.md) value can be added to a [Date](date.md) or [Date32](date32.md) value to produce a [DateTime](datetime.md) or [DateTime64](datetime64.md):

```sql
SET use_legacy_to_time = 0;
SELECT toDate('2024-07-15') + toTime('14:30:25') as datetime;
```

```text
┌────────────datetime─┐
1. │ 2024-07-15 14:30:25 │
└─────────────────────┘
```

See [Date and Time Addition](../operators/index.md#date-time-addition) for details on all supported combinations and result types.

## See Also {#see-also}

- [Type conversion functions](../functions/type-conversion-functions.md)
Expand Down
17 changes: 17 additions & 0 deletions docs/en/sql-reference/data-types/time64.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ SELECT CAST('14:30:25.250' AS Time64(3)) AS column, toTypeName(column) AS type;
└───────────────┴───────────┘
```

## Addition with Date {#addition-with-date}

A [Time64](time64.md) value can be added to a [Date](date.md) or [Date32](date32.md) value to produce a [DateTime64](datetime64.md) with the same scale as the `Time64`:

```sql
SET use_legacy_to_time = 0;
SELECT toDate('2024-07-15') + toTime64('14:30:25.123456', 6) AS dt, toTypeName(dt);
```

```text
┌─────────────────────────dt─┬─toTypeName(dt)─┐
1. │ 2024-07-15 14:30:25.123456 │ DateTime64(6) │
└────────────────────────────┴────────────────┘
```

See [Date and Time Addition](../operators/index.md#date-time-addition) for details on all supported combinations and result types.

**See Also**

- [Type conversion functions](../../sql-reference/functions/type-conversion-functions.md)
Expand Down
60 changes: 55 additions & 5 deletions docs/en/sql-reference/operators/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ SELECT * FROM a AS a1 JOIN a AS a2 ON a1.x <=> a2.x;
:::

The `<=>` operator is the `NULL`-safe equality operator, equivalent to `IS NOT DISTINCT FROM`.
It works like the regular equality operator (`=`), but it treats `NULL` values as comparable.
It works like the regular equality operator (`=`), but it treats `NULL` values as comparable.
Two `NULL` values are considered equal, and a `NULL` compared to any non-`NULL` value returns 0 (false) rather than `NULL`.

```sql
Expand Down Expand Up @@ -134,7 +134,7 @@ See [IN operators](../../sql-reference/operators/in.md) and [EXISTS](../../sql-r
`a GLOBAL NOT IN ...` – The `globalNotIn(a, b)` function.

### in subquery function {#in-subquery-function}
`a = ANY (subquery)` – The `in(a, subquery)` function.
`a = ANY (subquery)` – The `in(a, subquery)` function.

### notIn subquery function {#notin-subquery-function}
`a != ANY (subquery)` – The same as `a NOT IN (SELECT singleValueOrNull(*) FROM subquery)`.
Expand All @@ -143,7 +143,7 @@ See [IN operators](../../sql-reference/operators/in.md) and [EXISTS](../../sql-r
`a = ALL (subquery)` – The same as `a IN (SELECT singleValueOrNull(*) FROM subquery)`.

### notIn subquery function {#notin-subquery-function-1}
`a != ALL (subquery)` – The `notIn(a, subquery)` function.
`a != ALL (subquery)` – The `notIn(a, subquery)` function.

**Examples**

Expand Down Expand Up @@ -278,7 +278,7 @@ Types of intervals:

You can also use a string literal when setting the `INTERVAL` value. For example, `INTERVAL 1 HOUR` is identical to the `INTERVAL '1 hour'` or `INTERVAL '1' hour`.

:::tip
:::tip
Intervals with different types can't be combined. You can't use expressions like `INTERVAL 4 DAY 1 HOUR`. Specify intervals in units that are smaller or equal to the smallest unit of the interval, for example, `INTERVAL 25 HOUR`. You can use consecutive operations, like in the example below.
:::

Expand Down Expand Up @@ -314,7 +314,7 @@ SELECT now() AS current_date_time, current_date_time + INTERVAL '4' day + INTERV
└─────────────────────┴────────────────────────────────────────────────────────────┘
```

:::note
:::note
The `INTERVAL` syntax or `addDays` function are always preferred. Simple addition or subtraction (syntax like `now() + ...`) doesn't consider time settings. For example, daylight saving time.
:::

Expand All @@ -335,6 +335,56 @@ SELECT toDateTime('2014-10-26 00:00:00', 'Asia/Istanbul') AS time, time + 60 * 6
- [Interval](../../sql-reference/data-types/special-data-types/interval.md) data type
- [toInterval](/sql-reference/functions/type-conversion-functions#toIntervalYear) type conversion functions

### Date and Time Addition {#date-time-addition}

A [Date](../../sql-reference/data-types/date.md) or [Date32](../../sql-reference/data-types/date32.md) value can be added to a [Time](../../sql-reference/data-types/time.md) or [Time64](../../sql-reference/data-types/time64.md) value using the `+` operator. The result is a [DateTime](../../sql-reference/data-types/datetime.md) or [DateTime64](../../sql-reference/data-types/datetime64.md) representing the date at the given time of day. The operation is commutative.

The result type depends on the operand types:

| Left operand | Right operand | Result type |
|---|---|---|
| `Date` | `Time` | `DateTime` |
| `Date` | `Time64(s)` | `DateTime64(s)` |
| `Date32` | `Time` | `DateTime64(0)` |
| `Date32` | `Time64(s)` | `DateTime64(s)` |

:::note
The result uses the [session timezone](../../operations/settings/settings.md#session_timezone) (or server default timezone if no session timezone is set). The [`date_time_overflow_behavior`](../../operations/settings/settings-formats.md#date_time_overflow_behavior) setting controls what happens when the result is outside the representable range.
:::

Examples:

```sql
SET use_legacy_to_time = 0;
SELECT toDate('2024-07-15') + toTime('14:30:25') AS dt, toTypeName(dt);
```

```text
┌──────────────────dt─┬─toTypeName(dt)─┐
│ 2024-07-15 14:30:25 │ DateTime │
└─────────────────────┴────────────────┘
```

```sql
SELECT toDate('2024-07-15') + toTime64('14:30:25.123456', 6) AS dt, toTypeName(dt);
```

```text
┌─────────────────────────dt─┬─toTypeName(dt)─┐
│ 2024-07-15 14:30:25.123456 │ DateTime64(6) │
└────────────────────────────┴────────────────┘
```

```sql
SELECT toTime64('23:59:59.999', 3) + toDate32('2024-07-15') AS dt, toTypeName(dt);
```

```text
┌──────────────────────dt─┬─toTypeName(dt)─┐
│ 2024-07-15 23:59:59.999 │ DateTime64(3) │
└─────────────────────────┴────────────────┘
```

## Logical AND Operator {#logical-and-operator}

Syntax `SELECT a AND b` — calculates logical conjunction of `a` and `b` with the function [and](/sql-reference/functions/logical-functions#and).
Expand Down
Loading