Skip to content

Instantly share code, notes, and snippets.

@izidorome
Created July 22, 2019 22:32
Show Gist options
  • Select an option

  • Save izidorome/eeaacf728514b0786f389de8eea1f6b8 to your computer and use it in GitHub Desktop.

Select an option

Save izidorome/eeaacf728514b0786f389de8eea1f6b8 to your computer and use it in GitHub Desktop.
Mysql bin_to_uuid & uuid_to_bin
DELIMITER |
CREATE FUNCTION bin_to_uuid(b BINARY(16))
RETURNS CHAR(36) DETERMINISTIC
BEGIN
DECLARE HEX CHAR(32);
SET HEX = HEX(b);
RETURN LOWER(CONCAT(LEFT(HEX, 8), '-', MID(HEX, 9,4), '-', MID(HEX, 13,4), '-', MID(HEX, 17,4), '-', RIGHT(HEX, 12)));
END
|
CREATE FUNCTION uuid_to_bin(s CHAR(36))
RETURNS BINARY(16) DETERMINISTIC
RETURN UNHEX(CONCAT(LEFT(s, 8), MID(s, 10, 4), MID(s, 15, 4), MID(s, 20, 4), RIGHT(s, 12)))
|
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment