Solved

Update Table With Random Strings

Posted on 2008-10-17
3
774 Views
Last Modified: 2012-06-27
Dear Experts,

I have a table with 1,000,000 rows.  I have now created a new column in this table called random_strings where I somehow need to update the whole table so each of these 1,000,000 records has a random string in the new column.  How would I go about doing this?

The strings must be upper case and 5 characters in length.  However, the number of these 5 character strings needs to be random to per row separated by a space character, and there must be no more than 10 of these 5 character strings per row and no less than 1 of these 5 character strings per row.  For example

row1 = asdfg xcvbn
row2 = lkjdk eidkf dkeld ghdke slwyd
row3 = utire
...
row999998 =qqqqq asasa gfgre hggff nnnmm qqqqq asasa gfgre hggff nnnmm
row999999 = nnnnn aaaaa fffff
row1000000 = jfkdh eirut qywir fkdld ghfnd ppooi xcsas

Can anyone help me create a stored procedure to do this?
0
Comment
Question by:narmi2
3 Comments
 
LVL 5

Accepted Solution

by:
jfmador earned 500 total points
ID: 22746993
Hello to build your 5char value you can use

DECLARE @text as varchar(5)
SET @text = char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer))

To add a maximum of 10 string you store a random number in a Integer and use a while decreasing this value of one at each loop until it is equal to 0

DECLARE @count as integer
DECLARE @text as varchar(60)
SET @count = 1 + cast(rand() * 9 as integer)
while @count > 0
begin
SET @text = @Text + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + ' '
SET @Count = @Count - 1
end


You can put this code in a function and use an update statement to update all your row
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 37
How to update the value of duplicated records (except latest one) 2 18
SQL: launch actions one before the other 10 22
SQL Syntax 5 34
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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