Solved

Script to randomly create characters in MS SQL

Posted on 2016-11-27
6
56 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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