MySQL UUID_TO_BIN() Function
In MySQL, the UUID_TO_BIN() function converts a specified string UUID to a binary UUID and returns the result.
The UUID_TO_BIN() function is the reverse operation of the BIN_TO_UUID() function.
UUID_TO_BIN() Syntax
Here is the syntax of the MySQL UUID_TO_BIN() function:
UUID_TO_BIN(string_uuid, swap_flag)
Parameters
string_uuid- Required. A binary UUID.
swap_flag- Optional. The wap flag, available values are
0and1. The default is0.
Return value
The MySQL UUID_TO_BIN() function converts a specified string UUID to a binary UUID and returns the result.
If the argument swap_flag is 1, the UUID_TO_BIN() function will swap the time-low part and the time-high part in the UUID.
If the parameter string_uuid is NULL, the function will return NULL.
If any of the parameters are invalid, an error will be generated.
UUID_TO_BIN() Examples
This example shows the basic usage of the UUID_TO_BIN() function.
First, let’s create a string UUID as follow:
set @string_uuid = 'b45f7406-cf63-11ec-aeab-0242ac110003';
Then, let’s convert the binary UUID created above to a binary UUID:
SELECT UUID_TO_BIN(@string_uuid);
+---------------------------+
| UUID_TO_BIN(@string_uuid) |
+---------------------------+
| �_t�c쮫B� |
+---------------------------+Here, the binary data is not readable on the command line. We can convert it to a hex string using the HEX() function:
SELECT HEX(UUID_TO_BIN(@string_uuid));
+----------------------------------+
| HEX(UUID_TO_BIN(@string_uuid)) |
+----------------------------------+
| B45F7406CF6311ECAEAB0242AC110003 |
+----------------------------------+Of course, we can also use the BIN_TO_UUID() function:
SELECT BIN_TO_UUID(UUID_TO_BIN(@string_uuid));
+----------------------------------------+
| BIN_TO_UUID(UUID_TO_BIN(@string_uuid)) |
+----------------------------------------+
| b45f7406-cf63-11ec-aeab-0242ac110003 |
+----------------------------------------+Let’s swap the time-low part and the time-high part of the UUID using the swap_flag = 1 parameter :
SELECT HEX(UUID_TO_BIN(@string_uuid, 1));
+-----------------------------------+
| HEX(UUID_TO_BIN(@string_uuid, 1)) |
+-----------------------------------+
| 11ECCF63B45F7406AEAB0242AC110003 |
+-----------------------------------+Here, B45F7406 and 11EC were swapped.