Created
July 31, 2009 09:27
-
-
Save IcyMidnight/159151 to your computer and use it in GitHub Desktop.
MySQL functions to convert between binary and string uuids
This file contains hidden or 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 uuid_from_bin(b BINARY(16)) | |
RETURNS CHAR(36) DETERMINISTIC | |
BEGIN | |
DECLARE hex CHAR(32); | |
SET hex = HEX(b); | |
RETURN 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 ; |
zben
commented
Jan 31, 2014
Rename uuid_from_bin
to bin_to_uuid
so it matches mysql 8.0.
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