Skip to content

Instantly share code, notes, and snippets.

@jvalduvieco
Created February 1, 2015 19:23
Show Gist options
  • Save jvalduvieco/d9f004e07d2e83966fa5 to your computer and use it in GitHub Desktop.
Save jvalduvieco/d9f004e07d2e83966fa5 to your computer and use it in GitHub Desktop.
Convert UUID From bin to text and reverse
DELIMITER |
CREATE FUNCTION UuidFromBin(b BINARY(16))
RETURNS CHAR(36) DETERMINISTIC
BEGIN
DECLARE hex CHAR(32);
SET hex = HEX(b);
RETURN LCASE(CONCAT_WS('-',SUBSTR(hex,1, 8), SUBSTR(hex, 9,4), SUBSTR(hex, 13,4), SUBSTR(hex, 17,4), SUBSTR(hex, 21, 12)));
END
|
CREATE FUNCTION UuidToBin(s CHAR(36))
RETURNS BINARY(16) DETERMINISTIC
RETURN UNHEX(CONCAT(SUBSTR(s, 1, 8), SUBSTR(s, 10, 4), SUBSTR(s, 15, 4), SUBSTR(s, 20, 4), SUBSTR(s, 25, 12)))
|
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment