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
Solved

Update Table With Random Strings

Posted on 2008-10-17
3
775 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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