

# UUID functions

Use these functions to generate random or time-ordered UUIDs, and to read the version or the creation time that a UUID holds. The functions that generate a UUID return a new value on each call, so their results vary.

## `uuid` function
<a name="athena-duckdb-functions-utility-uuid"></a>

Returns a new random universally unique identifier (UUID) of version 4, as a value of type `UUID`. Each call returns a different value, even within one query, so when a query returns several rows, each row gets its own UUID. As text, a UUID is 36 characters long: 32 hexadecimal digits in five groups separated by hyphens.

`gen_random_uuid` and `uuidv4` are aliases of `uuid`. For a UUID that starts with its creation time, use the [`uuidv7` function](#athena-duckdb-functions-utility-uuidv7).

### Syntax
<a name="athena-duckdb-functions-utility-uuid-syntax"></a>

```sql
uuid()
gen_random_uuid()
uuidv4()
```

### Return type
<a name="athena-duckdb-functions-utility-uuid-return-type"></a>

`UUID`

### Examples
<a name="athena-duckdb-functions-utility-uuid-examples"></a>

The following SQL query shows properties of a new UUID. The value varies from run to run, so the query shows its version and its length as text instead of the value.

```sql
SELECT
    uuid_extract_version(uuid()) AS version,
    length(CAST(uuid() AS VARCHAR)) AS text_length
```

The query returns the following result.


| version | text\_length | 
| --- | --- | 
| `4` | `36` | 

The following SQL query creates a UUID for each of three rows and counts the distinct values.

```sql
SELECT count(DISTINCT u) AS distinct_values
FROM (
    SELECT
        uuid() AS u FROM (VALUES (1), (2), (3)) AS v(x)
) AS t
```

The query returns the following result.


| distinct\_values | 
| --- | 
| `3` | 

The following SQL query uses the `gen_random_uuid` alias and shows that two calls return different values.

```sql
SELECT gen_random_uuid() = gen_random_uuid() AS is_equal
```

The query returns the following result.


| is\_equal | 
| --- | 
| `false` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-utility-uuid-differences-from-engine-version-3"></a>

Athena engine version 3 cannot query a Hive table that has a `UUID` column. If you write the result of `uuid` to a Hive table that Athena engine version 3 or another engine also reads, cast it to `VARCHAR` first, as in `CAST(uuid() AS VARCHAR)`.

## `uuid_extract_timestamp` function
<a name="athena-duckdb-functions-utility-uuid-extract-timestamp"></a>

Returns the creation time that the version 7 UUID `uuid` holds in its first 48 bits, as a `TIMESTAMP WITH TIME ZONE` in UTC. A version 7 UUID stores its creation time in milliseconds, so the result has millisecond precision. If `uuid` is `NULL`, the result is `NULL`.

To create a version 7 UUID, use the [`uuidv7` function](#athena-duckdb-functions-utility-uuidv7). To find the version of a UUID, use the [`uuid_extract_version` function](#athena-duckdb-functions-utility-uuid-extract-version).

### Syntax
<a name="athena-duckdb-functions-utility-uuid-extract-timestamp-syntax"></a>

```sql
uuid_extract_timestamp({{uuid}})
```

### Arguments
<a name="athena-duckdb-functions-utility-uuid-extract-timestamp-arguments"></a>

The `uuid_extract_timestamp` function takes the following argument:

**{{uuid}}**  
The version 7 UUID to read the creation time from, as a `UUID` value. A string literal, such as `'019482e4-1441-7aad-8127-eec99573b0a0'`, is converted to `UUID` automatically. A `VARCHAR` column or other `VARCHAR` expression is not converted, so cast it first, as in `uuid_extract_timestamp(CAST({{column}} AS UUID))`.

### Return type
<a name="athena-duckdb-functions-utility-uuid-extract-timestamp-return-type"></a>

`TIMESTAMP WITH TIME ZONE`

### Examples
<a name="athena-duckdb-functions-utility-uuid-extract-timestamp-examples"></a>

The following SQL query returns the creation time of a version 7 UUID.

```sql
SELECT
    uuid_extract_timestamp(
        '019482e4-1441-7aad-8127-eec99573b0a0'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2025-01-20 08:46:13.057+00` | 

The following SQL query checks the version before it extracts the time. The UUID is version 1, so the result is `NULL` instead of an error.

```sql
SELECT
    CASE
        WHEN uuid_extract_version(u) = 7 THEN uuid_extract_timestamp(u)
    END AS result
FROM (VALUES
    (
        CAST(
            '12151fd2-7586-11e9-8f9e-2a86e4085a59' AS UUID
        )
    )
) AS v(u)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

### Considerations
<a name="athena-duckdb-functions-utility-uuid-extract-timestamp-considerations"></a>

If `uuid` is not a version 7 UUID, the query fails. When a column can hold UUIDs of other versions, check the version first, as in `CASE WHEN uuid_extract_version({{u}}) = 7 THEN uuid_extract_timestamp({{u}}) END`.

## `uuid_extract_version` function
<a name="athena-duckdb-functions-utility-uuid-extract-version"></a>

Returns the version number that is stored in `uuid`, such as `4` for a random UUID or `7` for a UUID that starts with its creation time. If `uuid` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-utility-uuid-extract-version-syntax"></a>

```sql
uuid_extract_version({{uuid}})
```

### Arguments
<a name="athena-duckdb-functions-utility-uuid-extract-version-arguments"></a>

The `uuid_extract_version` function takes the following argument:

**{{uuid}}**  
The value whose version you want, as a `UUID` value. A string literal, such as `'019482e4-1441-7aad-8127-eec99573b0a0'`, is converted to `UUID` automatically, and its text can use uppercase or lowercase letters, leave out the hyphens, or be enclosed in braces. A `VARCHAR` column or other `VARCHAR` expression is not converted, so cast it first, as in `uuid_extract_version(CAST({{column}} AS UUID))`.

### Return type
<a name="athena-duckdb-functions-utility-uuid-extract-version-return-type"></a>

`UINTEGER`

### Examples
<a name="athena-duckdb-functions-utility-uuid-extract-version-examples"></a>

The following SQL query returns the version of a version 7 UUID.

```sql
SELECT
    uuid_extract_version(
        '019482e4-1441-7aad-8127-eec99573b0a0'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `7` | 

The following SQL query returns the version of a version 1 UUID, written in uppercase letters.

```sql
SELECT
    uuid_extract_version(
        '12151FD2-7586-11E9-8F9E-2A86E4085A59'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1` | 

The following SQL query returns the versions of the UUIDs that `uuid` and `uuidv7` create.

```sql
SELECT
    uuid_extract_version(uuid()) AS uuid_version,
    uuid_extract_version(uuidv7()) AS uuidv7_version
```

The query returns the following result.


| uuid\_version | uuidv7\_version | 
| --- | --- | 
| `4` | `7` | 

### Considerations
<a name="athena-duckdb-functions-utility-uuid-extract-version-considerations"></a>

If `uuid` is a string that is not a valid UUID, the query fails.

## `uuidv7` function
<a name="athena-duckdb-functions-utility-uuidv7"></a>

Returns a new UUID of version 7, as a value of type `UUID`. Its first 48 bits hold the time at which the function created it, in milliseconds. Apart from the few bits that hold the version and the variant, the rest of its bits are random. Each call returns a different value, even within one query.

Because the creation time comes first, a UUID from a later millisecond compares as greater than one from an earlier millisecond. To read the creation time back, use the [`uuid_extract_timestamp` function](#athena-duckdb-functions-utility-uuid-extract-timestamp). For a random UUID that holds no creation time, use the [`uuid` function](#athena-duckdb-functions-utility-uuid).

### Syntax
<a name="athena-duckdb-functions-utility-uuidv7-syntax"></a>

```sql
uuidv7()
```

### Return type
<a name="athena-duckdb-functions-utility-uuidv7-return-type"></a>

`UUID`

### Examples
<a name="athena-duckdb-functions-utility-uuidv7-examples"></a>

The following SQL query shows the version of a new UUID. The value varies from run to run, so the query shows its version instead of the value.

```sql
SELECT uuid_extract_version(uuidv7()) AS version
```

The query returns the following result.


| version | 
| --- | 
| `7` | 

The following SQL query reads the creation time back from a new UUID and shows that it is within the last minute.

```sql
SELECT
    uuid_extract_timestamp(uuidv7()) > now() - INTERVAL 1 MINUTE AS is_recent
```

The query returns the following result.


| is\_recent | 
| --- | 
| `true` | 

The following SQL query compares two version 7 UUIDs whose creation times are one millisecond apart. The later UUID is greater.

```sql
SELECT
    CAST('019482e4-1441-7aad-8127-eec99573b0a0' AS UUID)
        < CAST(
            '019482e4-1442-7aad-8127-eec99573b0a0' AS UUID
        ) AS is_earlier
```

The query returns the following result.


| is\_earlier | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-utility-uuidv7-considerations"></a>

Two UUIDs from the same millisecond, such as two calls in one row, do not have a guaranteed order.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-utility-uuidv7-differences-from-engine-version-3"></a>

Athena engine version 3 cannot query a Hive table that has a `UUID` column. If you write the result of `uuidv7` to a Hive table that Athena engine version 3 or another engine also reads, cast it to `VARCHAR` first, as in `CAST(uuidv7() AS VARCHAR)`.