2020-04-03 13:23:32 +00:00
---
toc_priority: 53
2020-06-19 10:14:58 +00:00
toc_title: UUID
2020-04-03 13:23:32 +00:00
---
2020-04-30 18:19:18 +00:00
# Functions for Working with UUID {#functions-for-working-with-uuid}
2019-01-15 08:04:52 +00:00
The functions for working with UUID are listed below.
2020-03-21 04:11:51 +00:00
## generateUUIDv4 {#uuid-function-generate}
2019-01-15 08:04:52 +00:00
2020-04-30 18:19:18 +00:00
Generates the [UUID ](../../sql-reference/data-types/uuid.md ) of [version 4 ](https://tools.ietf.org/html/rfc4122#section-4.4 ).
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
generateUUIDv4()
```
**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 │
└──────────────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
## toUUID (x) {#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 │
└──────────────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
## UUIDStringToNum {#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={) │
└──────────────────────────────────────┴──────────────────┘
```
2020-03-20 10:10:48 +00:00
## UUIDNumToString {#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 │
└──────────────────┴──────────────────────────────────────┘
```
2020-04-30 18:19:18 +00:00
## See Also {#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 )
2019-01-15 08:04:52 +00:00
2020-01-30 10:34:55 +00:00
[Original article ](https://clickhouse.tech/docs/en/query_language/functions/uuid_function/ ) <!--hide-->