SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Generate Random characters
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pvccaz
Yak Posting Veteran

USA
87 Posts

Posted - 08/20/2013 :  20:34:33  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3661 Posts

Posted - 08/20/2013 :  20:54:01  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 08/21/2013 :  10:53:28  Show Profile  Reply with Quote
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

USA
87 Posts

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

madhivanan
Premature Yak Congratulator

India
22760 Posts

Posted - 08/26/2013 :  06:45:19  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000