Functions for working with UUIDs

```
SELECT
    toUUID('612f3c40-5d3b-217e-707b-6a546a3d7b29') AS uuid,
    UUIDToNum(uuid, 2) AS bytes
```

```
┌─uuid─────────────────────────────────┬─bytes────────────┐
│ 612f3c40-5d3b-217e-707b-6a546a3d7b29 │ @</a;]~!p{jTj={) │
└──────────────────────────────────────┴──────────────────┘
```

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

Introduced in: v24.5.0

Returns the timestamp component of a UUID version 7.

**Syntax**

```
UUIDv7ToDateTime(uuid[, timezone])
```

**Arguments**

- `uuid` — A UUID version 7. [`String`](/docs/sql-reference/data-types/string)
- `timezone` — Optional. [Timezone name](/docs/operations/server-configuration-parameters/settings#timezone) for the returned value. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a timestamp with milliseconds precision. If the UUID is not a valid version 7 UUID, it returns `1970-01-01 00:00:00.000`. [`DateTime64(3)`](/docs/sql-reference/data-types/datetime64)

**Examples**

**Usage example**

```
SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'))
```

```
┌─UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'))─┐
│                                          2024-04-22 15:30:29.048 │
└──────────────────────────────────────────────────────────────────┘
```

**With timezone**

```
SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 'America/New_York')
```

```
┌─UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 'America/New_York')─┐
│                                                             2024-04-22 11:30:29.048 │
└─────────────────────────────────────────────────────────────────────────────────────┘
```

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

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 [dateTime64ToSnowflakeID](#dateTime64ToSnowflakeID) instead.

Converts a [DateTime64](/docs/sql-reference/data-types/datetime64) to the first [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID) at the giving time.

**Syntax**

```
dateTime64ToSnowflake(value)
```

**Arguments**

- `value` — Date with time. [`DateTime64`](/docs/sql-reference/data-types/datetime64)

**Returned value**

Returns the input value converted as the first Snowflake ID at that time. [`Int64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
WITH toDateTime64('2021-08-15 18:57:56.492', 3, 'Asia/Shanghai') AS dt64 SELECT dateTime64ToSnowflake(dt64);
```

```
┌─dateTime64ToSnowflake(dt64)─┐
│         1426860704886947840 │
└─────────────────────────────┘
```

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

Introduced in: v24.6.0

Converts a [DateTime64](/docs/sql-reference/data-types/datetime64) value to the first [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID) at the giving time.

**Syntax**

```
dateTime64ToSnowflakeID(value[, epoch])
```

**Arguments**

- `value` — Date with time. [`DateTime64`](/docs/sql-reference/data-types/datetime64)
- `epoch` — 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)

**Returned value**

Input value converted to [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**simple**

```
SELECT dateTime64ToSnowflakeID(toDateTime64('2021-08-15 18:57:56', 3, 'Asia/Shanghai'))
```

```
6832626394434895872
```

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

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 [dateTimeToSnowflakeID](#dateTimeToSnowflakeID) instead.

Converts a [DateTime](/docs/sql-reference/data-types/datetime) value to the first [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID) at the giving time.

**Syntax**

```
dateTimeToSnowflake(value)
```

**Arguments**

- `value` — Date with time. [`DateTime`](/docs/sql-reference/data-types/datetime)

**Returned value**

Returns the input value as the first Snowflake ID at that time. [`Int64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
WITH toDateTime('2021-08-15 18:57:56', 'Asia/Shanghai') AS dt SELECT dateTimeToSnowflake(dt);
```

```
┌─dateTimeToSnowflake(dt)─┐
│     1426860702823350272 │
└─────────────────────────┘
```

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

Introduced in: v24.6.0

Converts a [DateTime](/docs/sql-reference/data-types/datetime) value to the first [Snowflake ID](https://en.wikipedia.org/wiki/Snowflake_ID) at the giving time.

**Syntax**

```
dateTimeToSnowflakeID(value[, epoch])
```

**Arguments**

- `value` — Date with time. [`DateTime`](/docs/sql-reference/data-types/datetime)
- `epoch` — 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)

**Returned value**

Input value converted to [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**simple**

```
SELECT dateTimeToSnowflakeID(toDateTime('2021-08-15 18:57:56', 'Asia/Shanghai'))
```

```
6832626392367104000
```

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

Introduced in: v25.9.0

Converts a [DateTime](/docs/sql-reference/data-types/datetime) value to a [UUIDv7](https://en.wikipedia.org/wiki/UUID#Version_7) at the given time.

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**

```
dateTimeToUUIDv7(value)
```

**Arguments**

- `value` — Date with time. [`DateTime`](/docs/sql-reference/data-types/datetime)

**Returned value**

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

**Examples**

**Usage example**

```
SELECT dateTimeToUUIDv7(toDateTime('2021-08-15 18:57:56', 'Asia/Shanghai'));
```

```
┌─dateTimeToUUIDv7(toDateTime('2021-08-15 18:57:56', 'Asia/Shanghai'))─┐
│ 018f05af-f4a8-778f-beee-1bedbc95c93b                                   │
└─────────────────────────────────────────────────────────────────────────┘
```

**multiple UUIDs for the same timestamp**

```
SELECT dateTimeToUUIDv7(toDateTime('2021-08-15 18:57:56'));
SELECT dateTimeToUUIDv7(toDateTime('2021-08-15 18:57:56'));
```

```
┌─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**