MySQL BIN_TO_UUID() Function

In MySQL, the BIN_TO_UUID() function converts a specified binary UUID to a string UUID and returns the result.

The BIN_TO_UUID() function is the reverse operation of the UUID_TO_BIN() function.

BIN_TO_UUID() Syntax

Here is the syntax of the MySQL BIN_TO_UUID() function:

BIN_TO_UUID(binary_uuid, swap_flag)

Parameters

binary_uuid
Required. A binary UUID.
swap_flag
Optional. The swap flag, available values are 0 and 1. The default is 0.

Return value

The MySQL BIN_TO_UUID() function converts a specified binary UUID to a string UUID and returns the result.

If the argument swap_flag is 1, the BIN_TO_UUID() function will swap the time-low part and the time-high part in the UUID.

If the parameter binary_uuid is NULL, the function will return NULL.

If any of the parameters are invalid, an error will occur.

BIN_TO_UUID() Examples

This example shows the basic usage of the BIN_TO_UUID() function.

First, let’s create a binary UUID as follows:

set @binary_uuid = UUID_TO_BIN('b45f7406-cf63-11ec-aeab-0242ac110003');

Then, let’s convert the binary UUID created above to a string UUID:

SELECT BIN_TO_UUID(@binary_uuid);
+--------------------------------------+
| BIN_TO_UUID(@binary_uuid)            |
+--------------------------------------+
| b45f7406-cf63-11ec-aeab-0242ac110003 |
+--------------------------------------+

Let’s swap the time-low part and the time-high part of the UUID using the parameter swap_flag = 1:

SELECT BIN_TO_UUID(@binary_uuid, 1);
+--------------------------------------+
| BIN_TO_UUID(@binary_uuid, 1)         |
+--------------------------------------+
| cf6311ec-7406-b45f-aeab-0242ac110003 |
+--------------------------------------+

Here, cf6311ec and b45f were swapped.