Solved

Script to randomly create characters in MS SQL

Posted on 2016-11-27
6
52 Views
Last Modified: 2016-11-28
We want to create a script that would create a value of 26 character length and have mixed characters from ascii value 65 thru 128.  We have so far:

select FLOOR(RAND(CHECKSUM(NEWID()))*(128-65))

Open in new window


But the values is lower that 65.

Also can't get to set for/next or loop to create a value of 26 random characters long.

Please advice.
0
Comment
Question by:rayluvs
[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
  • 3
  • 3
6 Comments
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41903626
Try.. This will give you different 26 characters each time.

SELECT LEFT(NEWID(),26)

O/p <<26 length>>

39AF35C7-8B7C-4BFD-8C76-93

Hope it helps !
0
 

Author Comment

by:rayluvs
ID: 41903628
Great!!!

We were still working on a solution (obviously your way better):
DECLARE @X INT=1, @v varchar
WAY:  --> Here the  DO statement
  SET @X += 1
  set @v=@v + 'h'
IF @X<=26 GOTO WAY
select  @v 'Value'

Open in new window


Thanx!!

Can you explain a bit on 'SELECT LEFT(NEWID(),26)'?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41903630
NewID() will give us unique value of 36 length. It is of data type uniqueidentifier.

After that we just taking 26 characters with the help of LEFT function.

Hope it helps !!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rayluvs
ID: 41903639
Thanx!
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41903640
Great ! Welcome

Regards,
Pawan
0
 

Author Closing Comment

by:rayluvs
ID: 41904387
(forgot yo award)

Thanx!
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.

739 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