Functions for working with UUIDs

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

- `value` — Snowflake ID. [`Int64`](/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 snowflakeToDateTime(CAST('1426860702823350272', 'Int64'), 'UTC');
```

```
┌─snowflakeToDateTime(CAST('1426860702823350272', 'Int64'), 'UTC')─┐
│                                              2021-08-15 10:57:56 │
└──────────────────────────────────────────────────────────────────┘
```

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

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

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

**Syntax**

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

**Arguments**

- `value` — Snowflake ID. [`Int64`](/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`. [`DateTime64(3)`](/docs/sql-reference/data-types/datetime64)

**Examples**

**Usage example**

```
SELECT snowflakeToDateTime64(CAST('1426860802823350272', 'Int64'), 'UTC');
```

```
┌─snowflakeToDateTime64(CAST('1426860802823350272', 'Int64'), 'UTC')─┐
│                                            2021-08-15 10:58:19.841 │
└────────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v21.1.0

Converts a String value to UUID type. If the conversion fails, returns a default UUID value instead of throwing an error.

This function attempts to parse a string of 36 characters in the standard UUID format (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
If the string cannot be converted to a valid UUID, the function returns the provided default UUID value.

**Syntax**

```
toUUIDOrDefault(string, default)
```

**Arguments**

- `string` — String of 36 characters or FixedString(36) to be converted to UUID. - `default` — UUID value to be returned if the first argument cannot be converted to UUID type.

**Returned value**

Returns the converted UUID if successful, or the default UUID if conversion fails. [`UUID`](/docs/sql-reference/data-types/uuid)

**Examples**

**Successful conversion returns the parsed UUID**

```
SELECT toUUIDOrDefault('61f0c404-5cb3-11e7-907b-a6006ad3dba0', toUUID('59f0c404-5cb3-11e7-907b-a6006ad3dba0'));
```

```
┌─toUUIDOrDefault('61f0c404-5cb3-11e7-907b-a6006ad3dba0', toUUID('59f0c404-5cb3-11e7-907b-a6006ad3dba0'))─┐
│ 61f0c404-5cb3-11e7-907b-a6006ad3dba0                                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**Failed conversion returns the default UUID**

```
SELECT toUUIDOrDefault('-----61f0c404-5cb3-11e7-907b-a6006ad3dba0', toUUID('59f0c404-5cb3-11e7-907b-a6006ad3dba0'));
```

```
┌─toUUIDOrDefault('-----61f0c404-5cb3-11e7-907b-a6006ad3dba0', toUUID('59f0c404-5cb3-11e7-907b-a6006ad3dba0'))─┐
│ 59f0c404-5cb3-11e7-907b-a6006ad3dba0                                                                          │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v20.12.0

Converts an input value to a value of type `UUID` but returns `NULL` in case of an error.

Like [`toUUID`](/docs/sql-reference/functions/type-conversion-functions#toUUID) but returns `NULL` instead of throwing an exception on conversion errors.

Supported arguments:

- String representations of UUID in standard format (8-4-4-4-12 hexadecimal digits).
- String representations of UUID without hyphens (32 hexadecimal digits).

Unsupported arguments (return `NULL`):

- Invalid string formats.
- Non-string types.
- Malformed UUIDs.

**Syntax**

```
toUUIDOrNull(x)
```

**Arguments**

- `x` — A string representation of a UUID. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns a UUID value if successful, otherwise `NULL`. [`UUID`](/docs/sql-reference/data-types/uuid) or [`NULL`](/docs/sql-reference/syntax#null)

**Examples**

**Usage examples**

```
SELECT
    toUUIDOrNull('550e8400-e29b-41d4-a716-446655440000') AS valid_uuid,
    toUUIDOrNull('invalid-uuid') AS invalid_uuid
```

```
┌─valid_uuid───────────────────────────┬─invalid_uuid─┐
│ 550e8400-e29b-41d4-a716-446655440000 │         ᴺᵁᴸᴸ │
└──────────────────────────────────────┴──────────────┘
```

- [UUIDv7 generation](#uuidv7-generation)- [Snowflake ID generation](#snowflake-id-generation)- [UUIDNumToString](#UUIDNumToString)- [UUIDStringToNum](#UUIDStringToNum)- [UUIDToNum](#UUIDToNum)- [UUIDv7ToDateTime](#UUIDv7ToDateTime)- [dateTime64ToSnowflake](#dateTime64ToSnowflake)- [dateTime64ToSnowflakeID](#dateTime64ToSnowflakeID)- [dateTimeToSnowflake](#dateTimeToSnowflake)- [dateTimeToSnowflakeID](#dateTimeToSnowflakeID)- [dateTimeToUUIDv7](#dateTimeToUUIDv7)- [generateSnowflakeID](#generateSnowflakeID)- [generateUUIDv4](#generateUUIDv4)- [generateUUIDv7](#generateUUIDv7)- [snowflakeIDToDateTime](#snowflakeIDToDateTime)- [snowflakeIDToDateTime64](#snowflakeIDToDateTime64)- [snowflakeToDateTime](#snowflakeToDateTime)- [snowflakeToDateTime64](#snowflakeToDateTime64)- [toUUIDOrDefault](#toUUIDOrDefault)- [toUUIDOrNull](#toUUIDOrNull)

Was this page helpful?