Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generate Random String

Posted on 2010-11-15
4
Medium Priority
?
1,083 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 1200 total points
ID: 34135970
select left(cast(newid() as varchar(36)),6)
0
 
LVL 14

Assisted Solution

by:Muhammad Ahmad Imran
Muhammad Ahmad Imran earned 400 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 400 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 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