Skip Navigation Links
Home
Functions
Tips & Tricks
SQL Server 2005
SQL Server 2008
SQL Server 2012
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : Generate Random Password User-Defined Function
Welcome to SQL Server Helper!!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

 
SQL Server Tip of the Day - October 21, 2014
Generate Random Password User-Defined Function

One question I saw in one of the SQL Server forums I usually visit is how to generate a random password using a user-defined function.  Here’s my version of the random password generator user-defined function.

Using the trick in the tip of the day entitled, “How to Call NEWID() Function in a User-Defined Function”, I was able to come up with the following user-defined function that generates a random password.

As discussed on the said tip of the day, I will need to call the NEWID() function within the user-defined function.  Since I cannot call the NEWID() function directly from the user-defined function, I need to create a view first:

CREATE VIEW [dbo].[RandomNewID]
AS
SELECT NEWID() AS [NewID]
GO

Now that I have this view, I can now create my user-defined function that will generate a random password.  To make the function more flexible, it accepts the number of characters that the user wants for the password:

CREATE FUNCTION [dbo].[ufn_GeneratePassword] ( @PasswordLength INT )
RETURNS VARCHAR(20)
AS
BEGIN

DECLARE @Password     VARCHAR(20)
DECLARE @ValidCharacters   VARCHAR(100)
DECLARE @PasswordIndex    INT
DECLARE @CharacterLocation   INT

SET @ValidCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890'

SET @PasswordIndex = 1
SET @Password = ''

WHILE @PasswordIndex <= @PasswordLength
BEGIN
 SELECT @CharacterLocation = ABS(CAST(CAST([NewID] AS VARBINARY) AS INT)) %
LEN(@ValidCharacters) + 1
 FROM [dbo].[RandomNewID]

 SET @Password = @Password + SUBSTRING(@ValidCharacters, @CharacterLocation, 1)

 SET @PasswordIndex = @PasswordIndex + 1
END

RETURN @Password

END

Here are some sample output using the generate password function:

SELECT [dbo].[ufn_GeneratePassword] ( 8 ) AS [Password8],
       [dbo].[ufn_GeneratePassword] ( 10 ) AS [Password10],
       [dbo].[ufn_GeneratePassword] ( 12 ) AS [Password12]

Password8 Password10 Password12
--------- ----------- ------------- rj58PffA 0x8dwB9peF iuLysW0zA2jy
 
SQL Server 2012
What's New in SQL Server 2012
SQL Server 2012 Articles
 
SQL Server 2008
What's New in SQL Server 2008

A collection of articles related to what's new with SQL Server 2008:

SQL Server 2008 Articles

A collection of articles related to SQL Server 2008:

 
User-Defined Functions
Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions
A collection of useful table-valued user-defined functions that can be used to join with other tables.
SQL Server Built-in Functions
A reference to all built-in functions available within SQL Server grouped into categories.
 
Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

 
Frequently Asked Questions
SQL Server

A collection of frequently asked questions about SQL Server grouped by categories:

SQL Server 2005

A collection of frequently asked questions about SQL Server 2005 grouped by categories: