How-to randomize in SQL

Few weeks ago I was task to populate dummy data into a table, I have to come up with a price and randomize the ID from 80-90, I have to populate this table with 25000 records!

So for solution, I turned to Rand(), an SQL function that randomizes integer, for quick reference here’s the simple syntax:

Rand [Seed]

a quick example would be :

Then I came up with my own


DECLARE @maxValue tinyint = 80
, @minValue tinyint = 90
, @randomId int
, @hid INT
, @amtadd INT;
SET @hid=0;
SET @amtadd=20;
WHILE @hid < 2000
SET @randomId = Cast(((@maxValue + 1) - @minValue) 
* Rand() + @minValue AS tinyint);
INSERT INTO tblDetail (PaymentId,PaymentDate, PaymentAmount,SeqNumber,IsActive,CreateDate,UpdateDate,UpdateUser)
VALUES (@randomId,'2013-10-17',@amtadd,3,1,getdate(),getdate(),'Alf')
SET @hid = @hid + 1;
SET @amtadd = Cast(((380 + 1) - 120) 
* Rand() + 120 AS int);


And there it is!

2 thoughts on “How-to randomize in SQL

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *