Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random field values in recordset

Status
Not open for further replies.

clivehenderson

IS-IT--Management
Nov 2, 2002
66
GB
Hi I'd like to set a field in every record of a dataset to a random value.

I have a php function which returns the random value.

so far I've had help to setup an update statement in a string which does the job but only produces a random number when the string is composed and copies the same number to all records.

I've been directed to this forum as there is probabbly a better way to do this.

I would appreciate any help at all.
Thanks
Clive
 
You can use rand() directly in your query.

UPDATE table_name set field_name=rand()
This will output a random floating point number into the column specified by field_name.

However if you want an integer, you may need to use truncate and manipulate the output a bit:

UPDATE table_name SET field_name=truncate((rand()*[red]1000[/red]),0)

The red number will need to change depending on how many rows you may have.








----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top