Random Password Generator

RAND() is not cryptographically secure and this script is limited to 10 characters. Fine for test data; for real service account passwords use the password manager’s generator.

DECLARE @length smallint =10
,@id INT
,@pwd VARCHAR (10) = ''

IF @length >10
begin
    raiserror ('Password Length too long',16,1,0)
    return
end

WHILE LEN(@pwd) < @length
BEGIN
SELECT @id = CAST(94 * RAND() + 33 AS DECIMAL (3,0))
IF @id IN (33,42) OR @id BETWEEN 35 AND 38 OR @id BETWEEN 48 AND 57 OR @id BETWEEN 63 AND 90 OR @id BETWEEN 97 AND 122
BEGIN
SET @pwd = @pwd + CHAR(@id)
END

END

SELECT len(@pwd),@pwd

From my SQL Server DBA handbook.