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

x
?
Solved

Script to randomly create characters in MS SQL

Posted on 2016-11-27
6
Medium Priority
?
70 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 32

Accepted Solution

by:
Pawan Kumar earned 2000 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 32

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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:rayluvs
ID: 41903639
Thanx!
0
 
LVL 32

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…

610 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