Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL random values

Posted on 2007-10-07
7
Medium Priority
?
1,069 Views
Last Modified: 2012-06-27
Hello all,

I am looking for the SQL statement that will update all the rows of my field
with random values between 30-100.
Thus, each record should be filled with a random value, but should not be
the same in all the records. It is ok if some records have the same values though.

Any ideas?
0
Comment
Question by:nectarios777
  • 4
  • 3
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20030543
random generic formula:
Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)

in your case:
Round(((289) * Rand() + 10), 0)

0
 
LVL 1

Author Comment

by:nectarios777
ID: 20030564
Hello angelIII,

here is what I'm using:

update tbTable
set totalratings =
Round(((289) * Rand() + 10), 0)


The problem is that all records are being updated with the same value.
I want each record to have a different value.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20030572
please try this:

update tbTable
set totalratings = ( select top 1
Round(((289) * Rand() + 10), 0)  from tbltable order by newid() )
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 1

Author Comment

by:nectarios777
ID: 20030604
Hello,

still the same problem
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20030631
0
 
LVL 1

Author Comment

by:nectarios777
ID: 20030698
AngelIII, have you got any idea how to make it return integers >1 rather than decimals?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20030705
simply combine the code in the link above with the formula posted first + casting:

create view wrapped_rand_view
as
select rand( ) as random_value
go
create function wrapped_rand()
returns int
as
begin
declare @f float
declare @r int
set @f = (select random_value from wrapped_rand_view)
set @r = Round(((289) * @f + 10), 0)
return @r
end
go
update t set d = dbo.wrapped_rand()


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

810 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