Solved

Generate Random String

Posted on 2010-11-15
4
1,078 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

688 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