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.
Author |
Topic |
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2008-10-29 : 15:46:22
|
Anyone have a nifty little scramble function? I'm turning over some data and want to scramble the actual names and addresses and phone numbers in order to create dummy data. I figure if I can use it in a update query life will be just grand!Thanks!!!Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-29 : 17:07:31
|
One possible way.... using cursordeclare @str varchar(8000), @numb int, @newstr varchar(8000)set @str = 'ABCDEFG 123'set @newstr = ''declare cur cursor forselect number from master..spt_values where type = 'P' and number between 1 and len(@str)order by newid()open curfetch next from cur into @numbwhile(@@FETCH_STATUS = 0)BEGIN select @newstr = substring(@str,@numb,1)+@newstr FETCH NEXT FROM cur into @numbENDprint @newstrclose curdeallocate cur |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2008-10-29 : 17:24:22
|
But Tara, the Almighty SQL Goddess, (tkizer) told me never to play with cursors! Can we do this as a function?Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-29 : 17:29:59
|
never play with fire unless you are a chef right?In this case, cursor is not used to perform update but to concatenate strings. There is no data access during the looping :D |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-29 : 17:51:39
|
I have another solution in SQL 2005, which is much better. You wanna see?There must be an easier way of doing this but, you'll need a view of random to make this work. The limitation is that your varchar can not be greater than 255.create view VW_RandomNumberas select number, newid() as randid from master..spt_values where type = 'P'crate function dbo.fn_scrumble(@str varchar(8000))returns varchar(8000)asBEGINdeclare @numb int, @newstr varchar(8000)--set @str = '123 MAIN STREET'set @newstr = ''declare cur cursor forselect number from vw_RandomNumber where number between 1 and len(@str)order by randidopen curfetch next from cur into @numbwhile(@@FETCH_STATUS = 0)BEGIN select @newstr = substring(@str,@numb,1)+@newstr FETCH NEXT FROM cur into @numbENDclose curdeallocate curreturn @newstrEND |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-10-29 : 19:07:41
|
This code will take 100000 randomly selected first names, last names, and addresses from a table, and combine them to produce 100000 randomly mixed combinations.Modify to suit your needs, but this should scale well to produce large sets of data.-- Get random selections of First Name, Last Name, and Addressselect top 100000 FIRST_NAME into #t1 from MyTable order by newid()select top 100000 LAST_NAME into #t2 from MyTable order by newid()select top 100000 STREET_ADDRESS1 into #t3 from MyTable order by newid()-- Add Seq column as lookup keyselect seq = identity(int,1,1), FIRST_NAME into #t1a from #t1 order by FIRST_NAMEselect seq = identity(int,1,1), LAST_NAME into #t2a from #t2 order by LAST_NAMEselect seq = identity(int,1,1), STREET_ADDRESS1 into #t3a from #t3 order by STREET_ADDRESS1-- Get 100000 random Seq in range of 1 to 100000 for each columnselect Seq1 = (abs(convert(int,convert(varbinary(16),newid())))%100000)+1 , Seq2 = (abs(convert(int,convert(varbinary(16),newid())))%100000)+1 , Seq3 = (abs(convert(int,convert(varbinary(16),newid())))%100000)+1into #t4from #t1-- Join to random Seqselect a.FIRST_NAME, b.LAST_NAME, c.STREET_ADDRESS1into #t5from #t4 x join #t1a a on x.Seq1 = a.Seq join #t2a b on x.Seq2 = b.Seq join #t3a c on x.Seq3 = c.Seq-- Randomly select 100 rowsselect top 100 * from #t5 order by newid()drop table #t1drop table #t2drop table #t3drop table #t4drop table #t5drop table #t1adrop table #t2adrop table #t3a CODO ERGO SUM |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-30 : 05:12:32
|
quote: Originally posted by hanbingl One possible way.... using cursordeclare @str varchar(8000), @numb int, @newstr varchar(8000)set @str = 'ABCDEFG 123'set @newstr = ''declare cur cursor forselect number from master..spt_values where type = 'P' and number between 1 and len(@str)order by newid()open curfetch next from cur into @numbwhile(@@FETCH_STATUS = 0)BEGIN select @newstr = substring(@str,@numb,1)+@newstr FETCH NEXT FROM cur into @numbENDprint @newstrclose curdeallocate cur
I would dodeclare @str varchar(8000), @numb intdeclare @t table(data varchar(1))set @str = 'ABCDEFG 123'insert into @tselect substring(@str,number,1) from master..spt_values where type = 'P' and number between 1 and len(@str)order by newid()declare @result varchar(8000)select @result=coalesce(@result,'')+data from @tselect @str as original,@result as scrambled MadhivananFailing to plan is Planning to fail |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2008-10-30 : 09:32:20
|
Madhivanan, hanbingl and Col. Jones.....thanks so much for your innovative solutions!!Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
|
|
|
|
|
|