Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to generate a random password

Stored Procedures

How to generate a random password

by  Malchik  Posted    (Edited  )
This SP will returns a random password composed of 3 uppercase letters, 2 lowercase letters and a number. It is easy to modify and use variables to control the lenght of each section

CREATE PROCEDURE p_GenerateRandomPassword(@PWD AS VARCHAR(15) OUTPUT)
AS

SET NOCOUNT ON
DECLARE @Count AS TINYINT

SET @Count = 0
SET @PWD = ''

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Randomize the first 3 letters in uppercase
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
WHILE @Count <= 2
BEGIN
SET @PWD = @PWD + CHAR(ROUND(((90 - 65 -1) * RAND() + 65), 0))
SET @Count = @Count + 1
END

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Randomize the next 2 letters in lowercase
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SET @Count = 0
WHILE @Count <= 1
BEGIN
SET @PWD = @PWD + LOWER(CHAR(ROUND(((90 - 65 -1) * RAND() + 65), 0)))
SET @Count = @Count + 1
END

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Randomize add a number
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SET @PWD = @PWD + CAST(ROUND(((25 - 1 -1) * RAND() + 9), 0) AS VARCHAR(3))
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top