12

I'm trying to get a random string in phpmyadmin using a function. I have the following code:

CREATE FUNCTION randomPassword()
RETURNS varchar(128)
BEGIN

    SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    SET @charLen = length(@chars);

    SET @randomPassword = '';

    WHILE length(@randomPassword) < 12
        SET @randomPassword = concat(@randomPassword, substring(@chars,CEILING(RAND() * @charLen),1));
    END WHILE;

    RETURN @randomPassword ;
END;

Now I get the error:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5

Does anyone know how I can fix this?

7
  • That specific message typically refers to missing parentheses, so that would be my guess. Commented Mar 10, 2015 at 11:19
  • END WHILE is wrong. You need only END at the end of a WHILE loop Commented Mar 10, 2015 at 11:20
  • It's my first function writing for mysql, so i have no idea what parentheses i'm missing? Commented Mar 10, 2015 at 11:21
  • If I was doing my first function, I'd start with something simpler!! Commented Mar 10, 2015 at 11:22
  • According to dev.mysql.com/doc/refman/5.0/en/while.html , mysql uses END WHILE Commented Mar 10, 2015 at 11:23

5 Answers 5

26

This is faster than concat + substring routine.

select substring(MD5(RAND()),1,20);

As I've tested inserting 1M random data, md5 routine consumes only 1/4 (even less) time of concat + substring routine;

The problem is a md5 string contains only 32 chars so if you need a longer one you'd have to manually generate more md5 strings and substring it yourself.

Sign up to request clarification or add additional context in comments.

Comments

11

Try this more simple solution:

SELECT CONV(FLOOR(RAND() * 99999999999999), 10, 36)

1 Comment

brilliant solution!
3
SELECT SUBSTRING(REPLACE(REPLACE(REPLACE( TO_BASE64(MD5(RAND())), '=',''),'+',''),'/',''), 2, 40)

This solution to generate a fixed length random string that contains all lower- and upper-case chars and digits.

SELECT SUBSTRING(REPLACE(REPLACE(REPLACE( TO_BASE64(MD5(RAND())), '=',''),'+',''),'/',''), 2, FLOOR(10+RAND()*31))

If you need a random length string (from 10 to 40 symbols in this example)

Comments

2

It's solved by using the Delimiter, i don't know for sure how, but it works Thanks

DELIMITER $$
CREATE FUNCTION randomPassword()
RETURNS varchar(128)
BEGIN

SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
SET @charLen = length(@chars);

SET @randomPassword = '';

WHILE length(@randomPassword) < 12
    DO
    SET @randomPassword = concat(@randomPassword, substring(@chars,CEILING(RAND() * @charLen),1));
END WHILE;

RETURN @randomPassword ;
END$$
DELIMITER ; 

Comments

0
CREATE FUNCTION randomPassword()
RETURNS varchar(128)
AS
BEGIN

declare @chars nvarchar(25);
declare @charlen int;
declare @randomPassword nvarchar(128);

SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
SET @charLen = len(@chars);

SET @randomPassword = '';

WHILE(LEN(@randomPassword) < 12)
BEGIN
    SET @randomPassword = concat(@randomPassword, substring(@chars,CEILING(RAND() * @charLen),1));
END

RETURN @randomPassword
END

5 Comments

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS BEGIN declare @chars nvarchar(25)' at line 3
Try to remove ..AS.. Maybe in MySql it works in this way
already did: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@chars nvarchar(25)' at line 5
substitute NVARCHAR to VARCHAR
oh yeah, should have seen that. but still gives the same error though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.