Solved

Generating a random string in Sql server

Posted on 2014-11-25
3
183 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
  • 2
3 Comments
 
LVL 65

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 14

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 65

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
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…

707 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