Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Generate Random characters

Author  Topic 

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2013-08-20 : 20:34:33
Hi,

I need to generate random strings each of length 7 characters and update it in a table that contains employee data.

For example. There is table called as Employee. It has Id, FirstName, LastName & Email as the columns. I need to mask the real names. So i have to generate random characters between a-z unique for individual records and update it in the table.

Input table

ID FirstName LastName Email
1 Glen Jackson test1@email.com
2 Christopher McClain test2@email.com
3 Carol Kholer test4@email.com
4 James Desoto test5@email.com

Output table should be:

ID FirstName LastName Email
1 Khjkltg Erstlkj test1@email.com
2 Asdlkfu Alsdkjf test2@email.com
3 Alsdkfl Aldkjfl test4@email.com
4 Basdfwe Asldkfj test5@email.com

I went through some websites for generating random characters. These provide a 7 or 8 character random letters.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=71874

But i do need to update 2 columns of an entire table with random characters.
Is there a good method to perform this. ?

Thanks in advance for your input.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-20 : 20:54:01
Use a random number generator - here is one way. Perhaps there are ways to shorten this, but it should work:
UPDATE YourTable SET
FirstName =
CHAR(65+ABS(CHECKSUM(NewId())) % 25) +
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25),
LastName =
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)+
CHAR(65+ABS(CHECKSUM(NewId())) % 25)
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-08-21 : 10:53:28
Dunno if this helps, but I tend to keep it simple and just use part of a GUID:
SELECT LEFT(REPLACE(CAST(NEWID() AS VARCHAR(36)), '-', ''), 7)


If you want to get fancy you can mae a table of names (first and last) and then cross join them to get a set of random names.
http://www.fakenamegenerator.com/order.php
http://listofrandomnames.com/
http://www.ssa.gov/OACT/babynames/
Go to Top of Page

pvccaz
Yak Posting Veteran

87 Posts

Posted - 2013-08-21 : 13:03:20
Thank you James and Lamprey. The samples that both of you provided works great !.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-08-26 : 06:45:19
This can also be of some help http://beyondrelational.com/modules/2/blogs/70/posts/10831/populating-sample-data.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -