Functions for working with UUID

The functions for working with UUID are listed below.

generateUUIDv4

Generates the UUID of version 4.

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

  1. CREATE TABLE t_uuid (x UUID) ENGINE=TinyLog
  2. INSERT INTO t_uuid SELECT generateUUIDv4()
  3. SELECT * FROM t_uuid
  1. ┌────────────────────────────────────x─┐
  2. f4bf890f-f9dc-4332-ad5c-0c18e73f28e9
  3. └──────────────────────────────────────┘

toUUID (x)

Converts String type value to UUID type.

  1. toUUID(String)

Returned value

The UUID type value.

Usage example

  1. SELECT toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0') AS uuid
  1. ┌─────────────────────────────────uuid─┐
  2. 61f0c404-5cb3-11e7-907b-a6006ad3dba0
  3. └──────────────────────────────────────┘

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

  1. UUIDStringToNum(String)

Returned value

FixedString(16)

Usage examples

  1. SELECT
  2. '612f3c40-5d3b-217e-707b-6a546a3d7b29' AS uuid,
  3. UUIDStringToNum(uuid) AS bytes
  1. ┌─uuid─────────────────────────────────┬─bytes────────────┐
  2. 612f3c40-5d3b-217e-707b-6a546a3d7b29 a/<@];!~p{jTj={)
  3. └──────────────────────────────────────┴──────────────────┘

UUIDNumToString

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

  1. UUIDNumToString(FixedString(16))

Returned value

String.

Usage example

  1. SELECT
  2. 'a/<@];!~p{jTj={)' AS bytes,
  3. UUIDNumToString(toFixedString(bytes, 16)) AS uuid
  1. ┌─bytes────────────┬─uuid─────────────────────────────────┐
  2. a/<@];!~p{jTj={) 612f3c40-5d3b-217e-707b-6a546a3d7b29
  3. └──────────────────┴──────────────────────────────────────┘

See also