Last active
February 13, 2023 05:30
-
-
Save thanglequoc/4a8011ec28066e1aa065fa2a15fe080f to your computer and use it in GitHub Desktop.
Revisions
-
thanglequoc renamed this gist
Feb 16, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
thanglequoc created this gist
Feb 16, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ DELIMITER $$ DROP FUNCTION IF EXISTS fn_generate_random_code $$ CREATE FUNCTION fn_generate_random_code (desired_code_len INTEGER) RETURNS VARCHAR(100) NO SQL BEGIN SET @possible_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; SET @len = LENGTH(@possible_characters); SET @random_code = ''; append_char_to_random_code: LOOP IF LENGTH(@random_code) >= desired_code_len THEN LEAVE append_char_to_random_code; END IF; SET @random_char_pos = FLOOR(RAND()*(@len - 0 + 1) + 0); SET @extracted_char = SUBSTRING(@possible_characters, @random_char_pos, 1); SET @random_code = CONCAT(@random_code, @extracted_char); END LOOP; RETURN @random_code; END $$