Link to home
Start Free TrialLog in
Avatar of nectarios777
nectarios777Flag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL random values

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?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

random generic formula:
Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)

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

Avatar of nectarios777

ASKER

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.
please try this:

update tbTable
set totalratings = ( select top 1
Round(((289) * Rand() + 10), 0)  from tbltable order by newid() )
Hello,

still the same problem
AngelIII, have you got any idea how to make it return integers >1 rather than decimals?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial