Created
February 1, 2015 19:23
-
-
Save jvalduvieco/d9f004e07d2e83966fa5 to your computer and use it in GitHub Desktop.
Convert UUID From bin to text and reverse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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