?
Solved

Generating a random string in Sql server

Posted on 2014-11-25
3
Medium Priority
?
217 Views
Last Modified: 2014-11-26
If I have 300 users in my database, and I need to assign each one a random Id,
how can I do this using SQL server. I don't want to use GUIDs because I think that is kind of over kill.

I would like the ID to be 4 characters long. I can query the database to find out if the id already exists, and if it does, I just run the random id sql code again, until I get a unique id.
0
Comment
Question by:brgdotnet
[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
  • 2
3 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1416 total points
ID: 40466077
Probably a more graceful way to pull this off, but this works
Declare @max smallint = 1, @min smallint = 26

SELECT 
   CHAR(((@max + 1) - @min) * Rand() + @min + 64) + 
   CHAR(((@max + 1) - @min) * Rand() + @min + 64) + 
   CHAR(((@max + 1) - @min) * Rand() + @min + 64) + 
   CHAR(((@max + 1) - @min) * Rand() + @min + 64)

Open in new window


btw Check out my article an article:  T-SQL recipe to create a million sample people that has a wompload of SQL Server random number/random name features.
0
 
LVL 15

Assisted Solution

by:Vikas Garg
Vikas Garg earned 584 total points
ID: 40466085
Hi,

You can use this along with your query

SELECT format(CAST(RAND() * 10000 AS int),'0000') AS RandomNumber , username from Usertable
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1416 total points
ID: 40466734
btw if you have some time to play with this, you could create a table with string values 'spring', 'summer', 'winter', 'fall', then mimic the article code I have to make your passwords a random string value and a couple of numbers.  

Will be easier to remember spring23 then UKEW.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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