Skip to content

Instantly share code, notes, and snippets.

@thanglequoc
Last active February 13, 2023 05:30
Show Gist options
  • Select an option

  • Save thanglequoc/4a8011ec28066e1aa065fa2a15fe080f to your computer and use it in GitHub Desktop.

Select an option

Save thanglequoc/4a8011ec28066e1aa065fa2a15fe080f to your computer and use it in GitHub Desktop.

Revisions

  1. thanglequoc renamed this gist Feb 16, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. thanglequoc created this gist Feb 16, 2020.
    18 changes: 18 additions & 0 deletions random_str_gen_script.sql
    Original 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 $$