Solved

Generate Random String

Posted on 2010-11-15
4
1,077 Views
Last Modified: 2012-06-27
In SQL Server, I need to generate a string (6 char length) that contain only alpha numeric and numeric character.

Such as : 3AC52C, or 308E2E or F30EDC

The string at least has 1 alpha numeric character and at least 1 numeric character.

Please help. Thanks.
0
Comment
Question by:softbless
4 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 300 total points
ID: 34135970
select left(cast(newid() as varchar(36)),6)
0
 
LVL 14

Assisted Solution

by:leoahmad
leoahmad earned 100 total points
ID: 34135975
The following stored procedure creates a random string of characters of a length specified by the parameter @Length:

CREATE PROCEDURE sp_GeneratePassword
(
    @Length int
)

AS

DECLARE @RandomID varchar(32)
DECLARE @counter smallint
DECLARE @RandomNumber float
DECLARE @RandomNumberInt tinyint
DECLARE @CurrentCharacter varchar(1)
DECLARE @ValidCharacters varchar(255)
SET @ValidCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+&$'
DECLARE @ValidCharactersLength int
SET @ValidCharactersLength = len(@ValidCharacters)
SET @CurrentCharacter = ''
SET @RandomNumber = 0
SET @RandomNumberInt = 0
SET @RandomID = ''

SET NOCOUNT ON

SET @counter = 1

WHILE @counter < (@Length + 1)

BEGIN

        SET @RandomNumber = Rand()
        SET @RandomNumberInt = Convert(tinyint, ((@ValidCharactersLength - 1) * @RandomNumber + 1))

        SELECT @CurrentCharacter = SUBSTRING(@ValidCharacters, @RandomNumberInt, 1)

        SET @counter = @counter + 1

        SET @RandomID = @RandomID + @CurrentCharacter

END

SELECT @RandomID AS 'Password'
GO

Open in new window

0
 
LVL 8

Assisted Solution

by:lomo74
lomo74 earned 100 total points
ID: 34153541
try this

with cte as (
	select
	case when rand() >= 0.5
		then char(65 + cast(rand() * 255 as int) % 26) -- letter
		else char(48 + cast(rand() * 255 as int) % 10) -- digit
	end +
	case when rand() >= 0.5
		then char(65 + cast(rand() * 255 as int) % 26)
		else char(48 + cast(rand() * 255 as int) % 10)
	end +
	case when rand() >= 0.5
		then char(65 + cast(rand() * 255 as int) % 26)
		else char(48 + cast(rand() * 255 as int) % 10)
	end +
	case when rand() >= 0.5
		then char(65 + cast(rand() * 255 as int) % 26)
		else char(48 + cast(rand() * 255 as int) % 10)
	end +
	case when rand() >= 0.5
		then char(65 + cast(rand() * 255 as int) % 26)
		else char(48 + cast(rand() * 255 as int) % 10)
	end +
	case when rand() >= 0.5
		then char(65 + cast(rand() * 255 as int) % 26)
		else char(48 + cast(rand() * 255 as int) % 10)
	end as pwd
)
-- ensure at least one digit and one letter,
-- since we can have obtained things like 123456 or ABCDEF
select	case
			when pwd not like '%[0-9]%' then left(char(48 + cast(rand() * 255 as int) % 10) + pwd, 6) -- prepend at least one digit if no digit present
			when pwd not like '%[A-Z]%' then left(char(65 + cast(rand() * 255 as int) % 26) + pwd, 6) -- prepend at least one letter if no letter present
			else pwd -- ok, pwd already composed of letters + digits
		end
from	cte

Open in new window

0
 

Author Closing Comment

by:softbless
ID: 34182918
Thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question