Solved

Script to randomly create characters in MS SQL

Posted on 2016-11-27
6
18 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 18

Accepted Solution

by:
Pawan Kumar Khowal 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 18

Expert Comment

by:Pawan Kumar Khowal
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:rayluvs
ID: 41903639
Thanx!
0
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41903640
Great ! Welcome

Regards,
Pawan
0
 

Author Closing Comment

by:rayluvs
ID: 41904387
(forgot yo award)

Thanx!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
CREATE DATABASE ENCRYPTION KEY 1 45
Params not declared?  T-SQL syntax question 3 22
Sql query 34 22
Join vs where 2 11
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…
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 …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now