Take you to understand MySQL random string functions

Source: Internet
Author: User

MySQL random string functions are commonly used. The syntax of this function is described in detail below. If you are interested in MySQL random string functions, take a look.

 
 
  1. DROP FUNCTION IF EXISTS rand_string;  
  2. delimiter //  
  3. CREATE FUNCTION rand_string(l_num tinyint UNSIGNED,l_type tinyint UNSIGNED)  
  4. RETURNS varchar(127)  
  5. BEGIN  
  6.  -- Function : rand_string  
  7.  -- Author   : dbachina#dbachina.com  
  8.  -- Date     : 2010/5/30  
  9.  -- l_num : The length of random string  
  10.  -- l_type: The string type  
  11.  --         1.0-9  
  12.  --         2.a-z  
  13.  --         3.A-Z  
  14.  --         4.a-zA-Z  
  15.  --         5.0-9a-zA-Z  
  16.  -- <for example> :  
  17.   -- mysql> select rand_string(12,5) random_string;  
  18.   -- +---------------+  
  19.   -- | random_string |  
  20.   -- +---------------+  
  21.   -- | 3KzGJCUJUplw  |  
  22.   -- +---------------+  
  23.   -- 1 row in set (0.00 sec)  
  24.  DECLARE i int UNSIGNED DEFAULT 0;  
  25.  DECLARE v_chars varchar(64) DEFAULT '0123456789';  
  26.   DECLARE result varchar ( 255) DEFAULT '';  
  27.  
  28.   IF l_type = 1 THEN  
  29.     SET v_chars = '0123456789';  
  30.   ELSEIF l_type = 2 THEN  
  31.     SET v_chars = 'abcdefghijklmnopqrstuvwxyz';  
  32.   ELSEIF l_type = 3 THEN  
  33.     SET v_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';  
  34.   ELSEIF l_type = 4 THEN  
  35.     SET v_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';  
  36.   ELSEIF l_type = 5 THEN  
  37.     SET v_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';  
  38.   ELSE  
  39.     SET v_chars = '0123456789';  
  40.   END IF;  
  41.  
  42.   WHILE i < l_num DO  
  43.       SET result = concat( result,substr(v_chars,ceil(rand()*(length(v_chars)-1)),1) );  
  44.     SET ii = i + 1;  
  45.   END WHILE;  
  46.   RETURN result;  
  47. END;  
  48. //  
  49. delimiter ;  

In-depth study of MySQL result strings

MySQL string truncation Function Method

Differences between MySQL string column types

Replace string with the MySQL replace Function

MySQL string addition function usage example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.