Functions for working with UUID

The functions for working with UUID are listed below.

generateUUIDv4

Generates the UUID of version 4.

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.

:) CREATE TABLE t_uuid (x UUID) ENGINE=TinyLog

:) INSERT INTO t_uuid SELECT generateUUIDv4()

:) SELECT * FROM t_uuid

┌────────────────────────────────────x─┐
 f4bf890f-f9dc-4332-ad5c-0c18e73f28e9 
└──────────────────────────────────────┘

toUUID (x)

Converts String type value to UUID type.

toUUID(String)

Returned value

The UUID type value.

Usage example

:) SELECT toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0') AS uuid

┌─────────────────────────────────uuid─┐
 61f0c404-5cb3-11e7-907b-a6006ad3dba0 
└──────────────────────────────────────┘

UUIDStringToNum

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).

UUIDStringToNum(String)

Returned value

FixedString(16)

Usage examples

:) SELECT 
    '612f3c40-5d3b-217e-707b-6a546a3d7b29' AS uuid, 
    UUIDStringToNum(uuid) AS bytes

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

UUIDNumToString

Accepts a FixedString(16) value, and returns a string containing 36 characters in text format.

UUIDNumToString(FixedString(16))

Returned value

String.

Usage example

SELECT 
    'a/<@];!~p{jTj={)' AS bytes, 
    UUIDNumToString(toFixedString(bytes, 16)) AS uuid

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

See also

Original article