Sample code for 30+ languages & platforms
SQL Server

Generating Random Password

See more PRNG Examples

Demonstrates how to generate random passwords.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    SELECT @success = 0

    DECLARE @fortuna int
    EXEC @hr = sp_OACreate 'Chilkat.Prng', @fortuna OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Set this equal to 1 if the generated password must include at least one digit (0-9)
    DECLARE @bDigit int
    SELECT @bDigit = 1

    -- Set this equal to 1 if the generated password must include both uppercase and lowercase chars.
    DECLARE @bUpperAndLower int
    SELECT @bUpperAndLower = 1

    -- The generated password must contain one of the following non-alphanumeric chars.
    DECLARE @otherChars nvarchar(4000)
    SELECT @otherChars = '@#$%*'

    -- Exclude chars that appear similar to others:
    DECLARE @excludeChars nvarchar(4000)
    SELECT @excludeChars = 'iIlLoO0'

    -- Generate 8-character passwords:
    DECLARE @i int

    SELECT @i = 1
    WHILE @i <= 10
      BEGIN
        EXEC sp_OAMethod @fortuna, 'RandomPassword', @sTmp0 OUT, 8, @bDigit, @bUpperAndLower, @otherChars, @excludeChars
        PRINT @sTmp0
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @fortuna


END
GO