MySQL BIN_TO_UUID() Function
MySQL BIN_TO_UUID() converts a 16-byte binary UUID to its 36-character string form. It is the reverse operation of UUID_TO_BIN().
Syntax
BIN_TO_UUID(binary_uuid [, swap_flag])
binary_uuid: A binary UUID, commonly returned byUUID_TO_BIN().swap_flag: Optional integer,0or1; the default is0.
The function returns NULL when binary_uuid is NULL. An invalid UUID value or flag causes an error.
With flag 0, MySQL interprets the bytes in their current order. With flag 1, it assumes the first and third UUID groups were swapped by UUID_TO_BIN(..., 1) and swaps them back when producing the string. Use the same flag in both directions.
Example: restore a UUID after storage
SET @uuid = '6ccd780c-baba-1026-9564-5b8c656024db';
SET @binary_uuid = UUID_TO_BIN(@uuid, 1);
SELECT BIN_TO_UUID(@binary_uuid, 1) AS restored_uuid;
The result is the original UUID, 6ccd780c-baba-1026-9564-5b8c656024db. Omitting the 1 on the BIN_TO_UUID() call would interpret the swapped bytes in the wrong order and return a different UUID.
BIN_TO_UUID() is designed to reverse the byte layout produced by UUID_TO_BIN(). Use swap_flag = 1 only for the version 1 layout; time-part swapping is not useful for UUIDs of other versions.
See MySQL’s official BIN_TO_UUID() documentation for conversion details.