Generate Random String Using MySQL


Have you ever wanted to generate a random string in MySQL, say for assigning a random password to a list of users?


SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6) AS passwordString

This generates a 6 character string where the characters are taken randomly from the MD5 command (and therefore in the range a-z and 0-9).

Enjoy………

Advertisements
By Jagat Kothari Posted in MySQL Tagged

3 comments on “Generate Random String Using MySQL

    • Hi,

      Using the given example, it will take combination of 6 diff. random characters and/or numbers.

      Still if to avoid the generation of same string, we can increase the no. of characters depending upon the requirement. that can reduce the chances of generating same string again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s