Generating random numbers in sql

Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

BlackHorseman

Thread Starter
Joined
Apr 27, 2002
Messages
451
Hi,

I need to perform the following query:

'update table table_name set field_name=cast(int, rand()*100+1)'

The problem is that rand() returns the same 'random' number for all fields.
Is there any way to generate efficient random numbers - and a different one for each entry to be updated?

D.
 
Joined
Aug 22, 2005
Messages
591
it sounds like you may be calling the rand function only once and updating all the fields with the returned number.
you can try a couple of things..
1. create variables for each of the fields you want to update, define each as a separate rand function call, and update the row using the variables.
2. repeat the update statement for each field separately?

which database are you using?
 
Joined
Jul 29, 2001
Messages
21,334
What is the random number it is generating each time? I was under the impression that you needed a range of numbers to use as the seed for the rnd function. I believe the function ins Rnd, not rand
 

BlackHorseman

Thread Starter
Joined
Apr 27, 2002
Messages
451
ms-sql
(sql server)

There are over half a million entries. I want to avoid having to update each manually.
As to your first suggestion - I am not experience enough in SQL to understand what you mean. Could you please give me a code example?

The basic statement is very simple:

'update table_name set field_name=[a random numeric value]'

Thanks,
D.
 
Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top