Skip to content

Instantly share code, notes, and snippets.

@ano
Created March 6, 2023 18:37
Show Gist options
  • Save ano/36d295cbee12c3ccc5ac0cddd2900af9 to your computer and use it in GitHub Desktop.
Save ano/36d295cbee12c3ccc5ac0cddd2900af9 to your computer and use it in GitHub Desktop.
a MySQL user defined function that takes in a number and uses it to generate a guid
# a MySQL user defined function that takes in a number and uses it to generate a guid
CREATE FUNCTION generateGuid(num INT) RETURNS CHAR(32)
BEGIN
DECLARE guid CHAR(32);
SET guid = CONCAT(
SUBSTR(SHA1(RAND()), 1, 8), '-',
SUBSTR(SHA1(RAND()), 1, 4), '-',
SUBSTR(SHA1(RAND()), 1, 4), '-',
SUBSTR(SHA1(RAND()), 1, 4), '-',
IF(LENGTH(num)>12, SUBSTR(SHA1(RAND()), 1, 12), CONCAT(REPEAT('0', 12-LENGTH(num)),num ) )
);
RETURN guid;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment