2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/uuid-functions
2022-04-09 13:29:05 +00:00
sidebar_position: 53
sidebar_label: UUID
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Functions for Working with UUID
2019-01-15 08:04:52 +00:00
The functions for working with UUID are listed below.
2022-06-02 10:55:18 +00:00
## generateUUIDv4
2019-01-15 08:04:52 +00:00
2021-08-10 11:11:41 +00:00
Generates the [UUID ](../data-types/uuid.md ) of [version 4 ](https://tools.ietf.org/html/rfc4122#section-4.4 ).
2019-01-15 08:04:52 +00:00
2022-05-22 23:17:45 +00:00
**Syntax**
2020-03-20 10:10:48 +00:00
``` sql
2022-05-22 23:17:45 +00:00
generateUUIDv4([x])
2019-01-15 08:04:52 +00:00
```
2022-05-22 23:17:45 +00:00
**Arguments**
- `x` — [Expression ](../../sql-reference/syntax.md#syntax-expressions ) resulting in any of the [supported data types ](../../sql-reference/data-types/index.md#data_types ). The resulting value is discarded, but the expression itself if used for bypassing [common subexpression elimination ](../../sql-reference/functions/index.md#common-subexpression-elimination ) if the function is called multiple times in one query. Optional parameter.
2019-01-15 08:04:52 +00:00
**Returned value**
The UUID type value.
**Usage example**
This example demonstrates creating a table with the UUID type column and inserting a value into the table.
2020-03-20 10:10:48 +00:00
``` sql
2019-09-23 15:31:46 +00:00
CREATE TABLE t_uuid (x UUID) ENGINE=TinyLog
2019-01-15 08:04:52 +00:00
2019-09-23 15:31:46 +00:00
INSERT INTO t_uuid SELECT generateUUIDv4()
2019-01-15 08:04:52 +00:00
2019-09-23 15:31:46 +00:00
SELECT * FROM t_uuid
```
2020-03-20 10:10:48 +00:00
``` text
2019-01-15 08:04:52 +00:00
┌────────────────────────────────────x─┐
│ f4bf890f-f9dc-4332-ad5c-0c18e73f28e9 │
└──────────────────────────────────────┘
```
2022-05-22 23:24:18 +00:00
**Usage example if it is needed to generate multiple values in one row**
2022-05-22 23:17:45 +00:00
```sql
SELECT generateUUIDv4(1), generateUUIDv4(2)
┌─generateUUIDv4(1)────────────────────┬─generateUUIDv4(2)────────────────────┐
│ 2d49dc6e-ddce-4cd0-afb8-790956df54c1 │ 8abf8c13-7dea-4fdf-af3e-0e18767770e6 │
└──────────────────────────────────────┴──────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## empty
2021-08-10 10:53:35 +00:00
Checks whether the input UUID is empty.
**Syntax**
```sql
empty(UUID)
```
The UUID is considered empty if it contains all zeros (zero UUID).
The function also works for [arrays ](array-functions.md#function-empty ) or [strings ](string-functions.md#empty ).
**Arguments**
2021-08-10 11:09:20 +00:00
- `x` — Input UUID. [UUID ](../data-types/uuid.md ).
2021-08-10 10:53:35 +00:00
**Returned value**
- Returns `1` for an empty UUID or `0` for a non-empty UUID.
Type: [UInt8 ](../data-types/int-uint.md ).
**Example**
2021-08-10 11:06:37 +00:00
To generate the UUID value, ClickHouse provides the [generateUUIDv4 ](#uuid-function-generate ) function.
2021-08-10 10:53:35 +00:00
Query:
```sql
SELECT empty(generateUUIDv4());
```
Result:
```text
┌─empty(generateUUIDv4())─┐
│ 0 │
└─────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## notEmpty
2021-08-10 10:53:35 +00:00
Checks whether the input UUID is non-empty.
**Syntax**
```sql
notEmpty(UUID)
```
The UUID is considered empty if it contains all zeros (zero UUID).
The function also works for [arrays ](array-functions.md#function-notempty ) or [strings ](string-functions.md#notempty ).
**Arguments**
2021-08-10 11:09:20 +00:00
- `x` — Input UUID. [UUID ](../data-types/uuid.md ).
2021-08-10 10:53:35 +00:00
**Returned value**
- Returns `1` for a non-empty UUID or `0` for an empty UUID.
Type: [UInt8 ](../data-types/int-uint.md ).
**Example**
2021-08-10 11:06:37 +00:00
To generate the UUID value, ClickHouse provides the [generateUUIDv4 ](#uuid-function-generate ) function.
2021-08-10 10:53:35 +00:00
Query:
```sql
SELECT notEmpty(generateUUIDv4());
```
Result:
```text
┌─notEmpty(generateUUIDv4())─┐
│ 1 │
└────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## toUUID (x)
2019-01-15 08:04:52 +00:00
2019-06-14 14:27:25 +00:00
Converts String type value to UUID type.
2019-01-15 08:04:52 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-01-15 08:04:52 +00:00
toUUID(String)
```
**Returned value**
The UUID type value.
**Usage example**
2020-03-20 10:10:48 +00:00
``` sql
2019-09-23 15:31:46 +00:00
SELECT toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0') AS uuid
```
2020-03-20 10:10:48 +00:00
``` text
2019-01-15 08:04:52 +00:00
┌─────────────────────────────────uuid─┐
│ 61f0c404-5cb3-11e7-907b-a6006ad3dba0 │
└──────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## toUUIDOrNull (x)
2020-10-25 08:45:29 +00:00
It takes an argument of type String and tries to parse it into UUID. If failed, returns NULL.
``` sql
toUUIDOrNull(String)
```
**Returned value**
2020-10-26 13:00:49 +00:00
The Nullable(UUID) type value.
2020-10-25 08:45:29 +00:00
**Usage example**
``` sql
SELECT toUUIDOrNull('61f0c404-5cb3-11e7-907b-a6006ad3dba0T') AS uuid
```
``` text
┌─uuid─┐
│ ᴺᵁᴸᴸ │
└──────┘
```
2022-06-02 10:55:18 +00:00
## toUUIDOrZero (x)
2020-10-25 08:45:29 +00:00
It takes an argument of type String and tries to parse it into UUID. If failed, returns zero UUID.
``` sql
toUUIDOrZero(String)
```
**Returned value**
The UUID type value.
**Usage example**
``` sql
SELECT toUUIDOrZero('61f0c404-5cb3-11e7-907b-a6006ad3dba0T') AS uuid
```
``` text
┌─────────────────────────────────uuid─┐
│ 00000000-0000-0000-0000-000000000000 │
└──────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## UUIDStringToNum
2019-01-15 08:04:52 +00:00
2020-04-30 18:19:18 +00:00
Accepts a string containing 36 characters in the format `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` , and returns it as a set of bytes in a [FixedString(16) ](../../sql-reference/data-types/fixedstring.md ).
2019-01-15 08:04:52 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-01-15 08:04:52 +00:00
UUIDStringToNum(String)
```
**Returned value**
FixedString(16)
**Usage examples**
2020-03-20 10:10:48 +00:00
``` sql
2019-09-23 15:31:46 +00:00
SELECT
2019-06-14 14:27:25 +00:00
'612f3c40-5d3b-217e-707b-6a546a3d7b29' AS uuid,
2019-01-15 08:04:52 +00:00
UUIDStringToNum(uuid) AS bytes
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-01-15 08:04:52 +00:00
┌─uuid─────────────────────────────────┬─bytes────────────┐
│ 612f3c40-5d3b-217e-707b-6a546a3d7b29 │ a/< @];!~p{jTj={) │
└──────────────────────────────────────┴──────────────────┘
```
2022-06-02 10:55:18 +00:00
## UUIDNumToString
2019-01-15 08:04:52 +00:00
2020-04-30 18:19:18 +00:00
Accepts a [FixedString(16) ](../../sql-reference/data-types/fixedstring.md ) value, and returns a string containing 36 characters in text format.
2019-01-15 08:04:52 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-01-15 08:04:52 +00:00
UUIDNumToString(FixedString(16))
```
**Returned value**
String.
**Usage example**
2020-03-20 10:10:48 +00:00
``` sql
2019-06-14 14:27:25 +00:00
SELECT
'a/< @];!~p{jTj={)' AS bytes,
2019-01-15 08:04:52 +00:00
UUIDNumToString(toFixedString(bytes, 16)) AS uuid
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-01-15 08:04:52 +00:00
┌─bytes────────────┬─uuid─────────────────────────────────┐
│ a/< @];!~p{jTj={) │ 612f3c40-5d3b-217e-707b-6a546a3d7b29 │
└──────────────────┴──────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## serverUUID()
2021-08-24 20:04:19 +00:00
2021-08-28 08:54:41 +00:00
Returns the random and unique UUID, which is generated when the server is first started and stored forever. The result writes to the file `uuid` created in the ClickHouse server directory `/var/lib/clickhouse/` .
2021-08-24 20:04:19 +00:00
**Syntax**
```sql
serverUUID()
```
**Returned value**
- The UUID of the server.
Type: [UUID ](../data-types/uuid.md ).
2022-06-02 10:55:18 +00:00
## See Also
2019-01-15 08:04:52 +00:00
2020-06-18 08:24:31 +00:00
- [dictGetUUID ](../../sql-reference/functions/ext-dict-functions.md#ext_dict_functions-other )