Solved

Generating a random string in Sql server

Posted on 2014-11-25
3
207 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 354 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 146 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 354 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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 video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

690 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