Solved

Script to randomly create characters in MS SQL

Posted on 2016-11-27
6
48 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
  • 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
Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Oracle IF statment 5 35
Comparison query - 4 columns 9 42
Using this function 4 39
Not listening to where 1 17
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this article I will describe the Copy Database Wizard 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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

763 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