Techiio-author
Started by Natasha JonesSep 30, 2021

Open
How do I Update a SQL Column with Random Numbers within a Range 1 - 10

1 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWES SHARE

I have a table named 'project1everything' and a column in that table named 'views' and I need to updated that column to have a random set of numbers from 1 - 10, each row with a different number. I tried looking around but nothing is working. I am using I assume the newest version of MySQL.

1 Replies

Techiio-commentatorNilima Paul replied 4 months ago0 likes0 dislikes

There is a RAND() function in TSQL but this function will generate a random number between 1 and 0 (a floating-point number).....

The below script can generate a random number based on the range of yours

Declare @maxRandomValue int

, @minRandomValue int ;

set @maxRandomValue = 1000

set @minRandomValue = 0

Select Cast( ( (@maxRandomValue + 1) - @minRandomValue)

* Rand() + @minRandomValue As int) As 'randomNumber'

now coming to the update query of yours, the below one is untested but give it a try...

Declare @maxRandomValue int

, @minRandomValue int ;

set @maxRandomValue = 1000

set @minRandomValue = 0

update <table> set <your_column> = Cast( ( (@maxRandomValue + 1) - @minRandomValue)

* Rand() + @minRandomValue As int) As 'randomNumber'

You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance