Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dr41d45/73965172405754b2ed7507c9a72a071e to your computer and use it in GitHub Desktop.
Save dr41d45/73965172405754b2ed7507c9a72a071e to your computer and use it in GitHub Desktop.
MySQL/MariaDB BIN_TO_UUID and UUID_TO_BIN Polyfill
DELIMITER //
CREATE FUNCTION BIN_TO_UUID(b BINARY(16))
RETURNS CHAR(36)
BEGIN
DECLARE hexStr CHAR(32);
SET hexStr = HEX(b);
RETURN LOWER(CONCAT(
SUBSTR(hexStr, 1, 8), '-',
SUBSTR(hexStr, 9, 4), '-',
SUBSTR(hexStr, 13, 4), '-',
SUBSTR(hexStr, 17, 4), '-',
SUBSTR(hexStr, 21)
));
END//
CREATE FUNCTION UUID_TO_BIN(uuid CHAR(36))
RETURNS BINARY(16)
BEGIN
RETURN UNHEX(REPLACE(uuid, '-', ''));
END//
DELIMITER ;
@dr41d45
Copy link
Author

dr41d45 commented Aug 27, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment