A blog about gadgets, media and all technology related ’stuff’. Be a geek for a few minutes ;)

SQL - How to generate a password

August 4th, 2008 by whit3kr0w

Password

If you have the need of a piece of SQL code to generate a password you can use this one:

declare @pw varchar(8)
declare @count int
declare @characterpool varchar(50)
set @characterpool = 'abcdefghijkmnpqrstuvwxyz0123456789'
set @count = 0
set @pw = ''
while @count <= 8
begin
set @pw = @pw + SUBSTRING(@characterpool,
CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@characterpool)+1,1)
set @count = @count + 1
end

This generates an eight digit password using the characters a-z and numbers. If you want you can change the length of the password by changing the ‘8′ to the desired number in the lines:

declare @pw varchar(8)
while @count <= 8

Hope you find this useful.

Thanks to GM for the code.

Posted in Programming

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.
Due to the amount of spam received in Geekie.net a Captcha system was implemented below. Please enter the 2 words before submitting your comment.