Raymond Lewallen

Sponsors

The Lounge

Wicked Cool Jobs

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Updated random password or string generator in T-Sql for Sql Server

Here is an updated version of my random string or password generator for T-Sql, made into a stored procedure with some other modifications.

The stored procedure

/***************************************************************************
* Created By: Raymond Lewallen
* Date:  05/16/2005
* Purpose: Generate a random string of given length
*
* Comments: Everything is self-explanatory.  Right now
*  max length is set to 100. So anything between 1
*  and 100 will work for a length.
*
*   If you specify a @charactersToUse,
*  the bit flags get ignored.
*
*   All spaces are stripped from the
*  @charactersToUse.
*
*   Characters can repeat. Will be
*  handled in a future version.
***************************************************************************/

CREATE procedure dbo.GenerateRandomString (
 @useNumbers bit,
 @useLowerCase bit,
 @useUpperCase bit,
 @charactersToUse as varchar(100),
 @passwordLength as smallint,
 @password varchar(100) OUT
)
As
Begin

if @passwordLength <= 0
 raiserror('Cannot generate a random string of zero length.',16,1)

declare @characters varchar(100)
declare @count int

set @characters = ''

if @useNumbers = 1
begin
 -- load up numbers 0 - 9
 set @count = 48
 while @count <=57
 begin
     set @characters = @characters + Cast(CHAR(@count) as char(1))
     set @count = @count + 1
 end
end

if @useLowerCase = 1
begin
 -- load up uppercase letters A - Z
 set @count = 65
 while @count <=90
 begin
     set @characters = @characters + Cast(CHAR(@count) as char(1))
     set @count = @count + 1
 end
end

if @useUpperCase = 1
begin
 -- load up lowercase letters a - z
 set @count = 97
 while @count <=122
 begin
     set @characters = @characters + Cast(CHAR(@count) as char(1))
     set @count = @count + 1
 end
end

set @count = 0
set @password = ''

-- If you specify a character set to use, the bit flags get ignored.
if Len(@charactersToUse) > 0
begin
 while charindex(@charactersToUse,' ') > 0
 begin
  set @charactersToUse = replace(@charactersToUse,' ','')
 end

 if Len(@charactersToUse) = 0
  raiserror('Cannot use an empty character set.',16,1)

 while @count <= @passwordLength
 begin
     set @password = @password + SUBSTRING(@charactersToUse,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@charactersToUse)+1,1)
     set @count = @count + 1
 end
end
else
begin
 while @count <= @passwordLength
 begin
     set @password = @password + SUBSTRING(@characters,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@characters)+1,1)
     set @count = @count + 1
 end
end

end
GO

And here are some examples of how to use it.

Use the bit flags - numbers only

declare @a varchar(20)
exec dbo.GenerateRandomString 1,0,0,null,20,@a OUT
print @a

Output: 44509955862560034316


Use the bit flags - entire character set

declare @a varchar(30)
exec dbo.GenerateRandomString 1,1,1,null,30,@a OUT
print @a

Output: bl4Dut6ACchq460u65j2fkYoalrAAN


Use a simple custom character set

declare @a varchar(50)
exec dbo.GenerateRandomString 1,1,1,'Ab',50,@a OUT
print @a

Output: bbbAAAbbbbAbbbAbbAAAbAbbbbbAbAbAbbAAAbAAbAbAbbAbbb


Use a more complex character setdeclare @a varchar(50)
exec dbo.GenerateRandomString 1,1,1,'ManagedCodeRocksMyWorld',50,@a OUT
print @a

Output: nednsndgcdgdnrgolRWRsgooRsdkMoCMRWedyglaknooodrdRd


Posted Tue, May 17 2005 5:21 AM by Raymond Lewallen

[Advertisement]

Comments

RIO - Randektív Informatikai Oldal wrote .::Szőkelizer 167::.
on Wed, May 18 2005 8:37 AM
&lt;ul&gt;
&lt;li&gt;&lt;a target=&quot;_blank&quot; href=&quot;http://desktop.google.com/enterprise&quot;&gt;Google Desktop for Enterprise&lt;/a&gt;
&lt;li&gt;&lt;a target=&quot;_blank&quot; href=&quot;http://www.microsoft.com/downloads/details.aspx?familyid=e315c516-2c2c-4870-a189-d47a5d7ffeb3&amp;#038;displaylang=en&quot;&gt;Excel
Ben wrote re: Updated random password or string generator in T-Sql for Sql Server
on Thu, Jun 9 2005 11:01 AM
Hey Raymond,

Thanks for the post. Your code came in quite handy and was a great help to me as a newby.

Ben
Raymond Lewallen wrote re: Updated random password or string generator in T-Sql for Sql Server
on Thu, Jun 9 2005 11:07 AM
Glad to hear it helped you out Ben :)
Raul wrote re: Updated random password or string generator in T-Sql for Sql Server
on Sun, Oct 9 2005 5:13 PM
Raymond
Thank you to provide your storage procedure..
Raymond I need to generate a ramdom string or number after a user submits its password using a text box on a web page. I believe your store procedure is the right solution in this case.
I also would like to provide back the password using email in case the user forgot his/her password, would you provide me some ideas how can I convert back the ramdom number generated using your store procedure to the original string the user submited.

I will appreciate any help.

Raul
tribune wrote re: Updated random password or string generator in T-Sql for Sql Server
on Mon, Oct 17 2005 2:03 AM
awesome procedure! cheers
K Sub wrote re: Updated random password or string generator in T-Sql for Sql Server
on Thu, Dec 1 2005 4:15 PM
Thank you. I found it very useful.
Arjarapu Blog Space » SQL SERVER Generate Password Function wrote Arjarapu Blog Space &raquo; SQL SERVER Generate Password Function
on Tue, May 2 2006 11:45 AM
Wes F. wrote re: Updated random password or string generator in T-Sql for Sql Server
on Fri, Sep 8 2006 10:35 AM

Thank you so much for this great procedure! Exactly what I was looking for!

Steve wrote re: Updated random password or string generator in T-Sql for Sql Server
on Wed, Sep 20 2006 9:06 PM

Thanks - this is exactly what I was looking for as well.  I did find a bug in the procedure, however.  Whatever length you pass in will return a string 1 character longer.   Bot "while @count <= @passwordLength" lines in the procedure need to simply use "<" since @count is initialized to 0.

Carl wrote re: Updated random password or string generator in T-Sql for Sql Server
on Thu, Oct 5 2006 4:31 AM

Many thanks, saved lots of work. Flexible yet simple - great!

komp wrote re: Updated random password or string generator in T-Sql for Sql Server
on Fri, Oct 13 2006 10:02 AM

Excellent work. I appreciate it. It is the one which I am looking for. Saved me lots of time. Thanks

Chris Beck wrote re: Updated random password or string generator in T-Sql for Sql Server
on Fri, Nov 10 2006 5:32 PM

Thanks - very helpful.

One defect: the application of "use upper case" and "use lower case" are reversed.

Kathryn wrote re: Updated random password or string generator in T-Sql for Sql Server
on Thu, May 3 2007 4:26 AM

Two years on and this post is still a useful resource for people. Thanks for helping to make my task today just that little bit easier!

Atul wrote re: Updated random password or string generator in T-Sql for Sql Server
on Sat, Jun 2 2007 2:15 AM

This is really perfect for generating a random string.

Thanks a lot for such a nice code.

Jason wrote re: Updated random password or string generator in T-Sql for Sql Server
on Tue, Jul 3 2007 6:42 PM

Just what I needed. Thanks!

WebMart Blog » SQL Server 2005 function to create a random string wrote WebMart Blog &raquo; SQL Server 2005 function to create a random string
on Mon, Oct 22 2007 1:49 PM

Pingback from  WebMart Blog &raquo; SQL Server 2005 function to create a random string

Jens Peter Winkler wrote re: Updated random password or string generator in T-Sql for Sql Server
on Wed, Nov 7 2007 9:51 AM

Very useful, thanks a lot. I have used the procedure for obfuscating personal data.

matt wrote re: Updated random password or string generator in T-Sql for Sql Server
on Tue, Dec 11 2007 1:53 PM

Excellent job with this procedure.. very useful!

I noticed your Upper/Lower case checks are reversed (the if @useUpperCase = 1 generates the lower case characters, etc). Also, the @count should start at 1 instead of zero when creating the string, otherwise the resulting string is 1 character longer than what you requested. It doesn't show up in you examples because the string length is explicit.

Alex wrote re: Updated random password or string generator in T-Sql for Sql Server
on Wed, Jan 30 2008 4:07 PM
Nice work! Thanks.
Chandra Mouli wrote re: Updated random password or string generator in T-Sql for Sql Server
on Wed, Apr 16 2008 3:09 AM

Wow... A perfect Procedure for generating Password or PIN.

Marius Smith wrote re: Updated random password or string generator in T-Sql for Sql Server
on Wed, May 7 2008 12:03 PM

Thanks Raymond. I changed your code to allow for the removal of duplicates to use for myself .

Could your verify this is what you meant as my home language is not English?

Below is the code (changes is at the end where you were setting the password?)

CREATE procedure dbo.GenerateRandomString (

@useNumbers bit,

@useLowerCase bit,

@useUpperCase bit,

@charactersToUse as varchar(100),

@passwordLength as smallint,

@password varchar(100) OUT

)

As

Begin

if @passwordLength <= 0

raiserror('Cannot generate a random string of zero length.',16,1)

declare @characters varchar(100)

declare @count int

set @characters = ''

if @useNumbers = 1

begin

-- load up numbers 0 - 9

set @count = 48

while @count <=57

begin

    set @characters = @characters + Cast(CHAR(@count) as char(1))

    set @count = @count + 1

end

end

if @useLowerCase = 1

begin

-- load up uppercase letters A - Z

set @count = 65

while @count <=90

begin

    set @characters = @characters + Cast(CHAR(@count) as char(1))

    set @count = @count + 1

end

end

if @useUpperCase = 1

begin

-- load up lowercase letters a - z

set @count = 97

while @count <=122

begin

    set @characters = @characters + Cast(CHAR(@count) as char(1))

    set @count = @count + 1

end

end

set @count = 0

set @password = ''

-- If you specify a character set to use, the bit flags get ignored.

if Len(@charactersToUse) > 0

begin

while charindex(@charactersToUse,' ') > 0

begin

 set @charactersToUse = replace(@charactersToUse,' ','')

end

if Len(@charactersToUse) = 0

 raiserror('Cannot use an empty character set.',16,1)

while @count <= @passwordLength

begin

    set @password = @password + SUBSTRING(@charactersToUse,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@charactersToUse)+1,1)

    set @charactersToUse = left(@charactersToUse,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@charactersToUse)) + right(@charactersToUse,LEN(@charactersToUse)- CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@charactersToUse)+2)

    set @count = @count + 1

end

end

else

begin

while @count <= @passwordLength

begin

    set @password = @password + SUBSTRING(@characters,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@characters)+1,1)

    set @count = @count + 1

end

end

end

GO

Ashwini wrote re: Updated random password or string generator in T-Sql for Sql Server
on Thu, Sep 4 2008 5:34 AM

Thanks for giving me nice solution

sa password | keyongtech wrote sa password | keyongtech
on Wed, Jan 21 2009 11:25 PM

Pingback from  sa password | keyongtech

Rajesh Kalathilayil wrote re: Updated random password or string generator in T-Sql for Sql Server
on Fri, Jan 23 2009 5:28 AM

Thank you very much!

zuf?lliges Password generieren | hilpers wrote zuf?lliges Password generieren | hilpers
on Fri, Feb 20 2009 8:06 AM

Pingback from  zuf?lliges Password generieren | hilpers

Anil Gupta wrote re: Updated random password or string generator in T-Sql for Sql Server
on Mon, Apr 27 2009 2:00 AM

Thanks a lot.

you saved me lot of time.

JAV! wrote re: Updated random password or string generator in T-Sql for Sql Server
on Tue, May 26 2009 3:07 AM

Thanks a lot. Great job!!

HJ wrote re: Updated random password or string generator in T-Sql for Sql Server
on Sat, Oct 17 2009 10:58 AM

Thank you. Great procedure.

Alex wrote re: Updated random password or string generator in T-Sql for Sql Server
on Tue, Nov 10 2009 6:39 PM

How can I use it to update 2000 users, I want to set the wed_id as the random alphanumeric.

could you please help me?

how to create random numbers? | it.rss24h.com wrote how to create random numbers? | it.rss24h.com
on Wed, Nov 11 2009 12:33 AM

Pingback from  how to create random numbers? | it.rss24h.com

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us