Functions for working with UUIDs

```
┌─dateTimeToUUIDv7(t⋯08-15 18:57:56'))─┐
│ 017b4b2d-7720-76ed-ae44-bbcc23a8c550 │
└──────────────────────────────────────┘
┌─dateTimeToUUIDv7(t⋯08-15 18:57:56'))─┐
│ 017b4b2d-7720-76ed-ae44-bbcf71ed0fd3 │
└──────────────────────────────────────┘
```

## generateSnowflakeID[​](#generateSnowflakeID "Direct link to generateSnowflakeID")

Introduced in: v24.6.0

Generates a [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID).

Function `generateSnowflakeID` guarantees that the counter field within a timestamp increments monotonically across all function invocations in concurrently running threads and queries.

See section ["Snowflake ID generation"](#snowflake-id-generation) for implementation details.

**Syntax**

```
generateSnowflakeID([expr, [machine_id]])
```

**Arguments**

- `expr` — An arbitrary [expression](/docs/sql-reference/syntax#expressions) used to bypass [common subexpression elimination](/docs/sql-reference/functions/overview#common-subexpression-elimination) if the function is called multiple times in a query. The value of the expression has no effect on the returned Snowflake ID. Optional. - `machine_id` — A machine ID, the lowest 10 bits are used. [Int64](/docs/sql-reference/data-types/int-uint). Optional.

**Returned value**

Returns the Snowflake ID. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
CREATE TABLE tab (id UInt64)
ENGINE = MergeTree()
ORDER BY tuple();

INSERT INTO tab SELECT generateSnowflakeID();

SELECT * FROM tab;
```

```
┌──────────────────id─┐
│ 7199081390080409600 │
└─────────────────────┘
```

**Multiple Snowflake IDs generated per row**

```
SELECT generateSnowflakeID(1), generateSnowflakeID(2);
```

```
┌─generateSnowflakeID(1)─┬─generateSnowflakeID(2)─┐
│    7199081609652224000 │    7199081609652224001 │
└────────────────────────┴────────────────────────┘
```

**With expression and a machine ID**

```
SELECT generateSnowflakeID('expr', 1);
```

```
┌─generateSnowflakeID('expr', 1)─┐
│            7201148511606784002 │
└────────────────────────────────┘
```

## generateUUIDv4[​](#generateUUIDv4 "Direct link to generateUUIDv4")

Introduced in: v1.1.0

Generates a [version 4](https://tools.ietf.org/html/rfc4122#section-4.4) [UUID](/docs/sql-reference/data-types/uuid).

**Syntax**

```
generateUUIDv4([expr])
```

**Arguments**

- `expr` — Optional. An arbitrary expression used to bypass [common subexpression elimination](/docs/sql-reference/functions/overview#common-subexpression-elimination) if the function is called multiple times in a query. The value of the expression has no effect on the returned UUID.

**Returned value**

Returns a UUIDv4. [`UUID`](/docs/sql-reference/data-types/uuid)

**Examples**

**Usage example**

```
SELECT generateUUIDv4(number) FROM numbers(3);
```

```
┌─generateUUIDv4(number)───────────────┐
│ fcf19b77-a610-42c5-b3f5-a13c122f65b6 │
│ 07700d36-cb6b-4189-af1d-0972f23dc3bc │
│ 68838947-1583-48b0-b9b7-cf8268dd343d │
└──────────────────────────────────────┘
```

**Common subexpression elimination**

```
SELECT generateUUIDv4(1), generateUUIDv4(1);
```

```
┌─generateUUIDv4(1)────────────────────┬─generateUUIDv4(2)────────────────────┐
│ 2d49dc6e-ddce-4cd0-afb8-790956df54c1 │ 2d49dc6e-ddce-4cd0-afb8-790956df54c1 │
└──────────────────────────────────────┴──────────────────────────────────────┘
```

## generateUUIDv7[​](#generateUUIDv7 "Direct link to generateUUIDv7")

Introduced in: v24.5.0

Generates a [version 7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04) [UUID](/docs/sql-reference/data-types/uuid).

See section ["UUIDv7 generation"](#uuidv7-generation) for details on UUID structure, counter management, and concurrency guarantees.

Note

As of September 2025, version 7 UUIDs are in draft status and their layout may change in future.

**Syntax**

```
generateUUIDv7([expr])
```

**Arguments**

- `expr` — Optional. An arbitrary expression used to bypass [common subexpression elimination](/docs/sql-reference/functions/overview#common-subexpression-elimination) if the function is called multiple times in a query. The value of the expression has no effect on the returned UUID. [`Any`](/docs/sql-reference/data-types)

**Returned value**

Returns a UUIDv7. [`UUID`](/docs/sql-reference/data-types/uuid)

**Examples**

**Usage example**

```
SELECT generateUUIDv7(number) FROM numbers(3);
```

```
┌─generateUUIDv7(number)───────────────┐
│ 019947fb-5766-7ed0-b021-d906f8f7cebb │
│ 019947fb-5766-7ed0-b021-d9072d0d1e07 │
│ 019947fb-5766-7ed0-b021-d908dca2cf63 │
└──────────────────────────────────────┘
```

**Common subexpression elimination**

```
SELECT generateUUIDv7(1), generateUUIDv7(1);
```

```
┌─generateUUIDv7(1)────────────────────┬─generateUUIDv7(1)────────────────────┐
│ 019947ff-0f87-7d88-ace0-8b5b3a66e0c1 │ 019947ff-0f87-7d88-ace0-8b5b3a66e0c1 │
└──────────────────────────────────────┴──────────────────────────────────────┘
```

## snowflakeIDToDateTime[​](#snowflakeIDToDateTime "Direct link to snowflakeIDToDateTime")

Introduced in: v24.6.0

Returns the timestamp component of a [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID) as a value of type [DateTime](/docs/sql-reference/data-types/datetime).

**Syntax**

```
snowflakeIDToDateTime(value[, epoch[, time_zone]])
```

**Arguments**

- `value` — Snowflake ID. [`UInt64`](/docs/sql-reference/data-types/int-uint)
- `epoch` — Optional. Epoch of the Snowflake ID in milliseconds since 1970-01-01. Defaults to 0 (1970-01-01). For the Twitter/X epoch (2015-01-01), provide 1288834974657. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `time_zone` — Optional. [Timezone](/docs/operations/server-configuration-parameters/settings#timezone). The function parses `time_string` according to the timezone. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the timestamp component of `value`. [`DateTime`](/docs/sql-reference/data-types/datetime)

**Examples**

**Usage example**

```
SELECT snowflakeIDToDateTime(7204436857747984384) AS res
```

```
┌─────────────────res─┐
│ 2024-06-06 10:59:58 │
└─────────────────────┘
```

## snowflakeIDToDateTime64[​](#snowflakeIDToDateTime64 "Direct link to snowflakeIDToDateTime64")

Introduced in: v24.6.0

Returns the timestamp component of a [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID) as a value of type [DateTime64](/docs/sql-reference/data-types/datetime64).

**Syntax**

```
snowflakeIDToDateTime64(value[, epoch[, time_zone]])
```

**Arguments**

- `value` — Snowflake ID. [`UInt64`](/docs/sql-reference/data-types/int-uint)
- `epoch` — Optional. Epoch of the Snowflake ID in milliseconds since 1970-01-01. Defaults to 0 (1970-01-01). For the Twitter/X epoch (2015-01-01), provide 1288834974657. [`UInt*`](/docs/sql-reference/data-types/int-uint)
- `time_zone` — Optional. [Timezone](/docs/operations/server-configuration-parameters/settings#timezone). The function parses `time_string` according to the timezone. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the timestamp component of `value` as a `DateTime64` with scale = 3, i.e. millisecond precision. [`DateTime64`](/docs/sql-reference/data-types/datetime64)

**Examples**

**Usage example**

```
SELECT snowflakeIDToDateTime64(7204436857747984384) AS res
```

```
┌─────────────────res─┐
│ 2024-06-06 10:59:58 │
└─────────────────────┘
```

## snowflakeToDateTime[​](#snowflakeToDateTime "Direct link to snowflakeToDateTime")

Introduced in: v21.10.0

Deprecated feature

Note

This function is deprecated and can only be used if setting [`allow_deprecated_snowflake_conversion_functions`](/docs/operations/settings/settings#allow_deprecated_snowflake_conversion_functions) is enabled.
The function will be removed at some point in future.

Please use function [`snowflakeIDToDateTime`](#snowflakeIDToDateTime) instead.

Extracts the timestamp component of a [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID) in [DateTime](/docs/sql-reference/data-types/datetime) format.

**Syntax**

```
snowflakeToDateTime(value[, time_zone])
```

**Arguments**