Generate Random Numbers with Sql server

1
19

Image

 

 

 

 

 

 

 

select RAND()

will return a Random Number , at the time of writing this i got

0.146565669485781

if we need to get Random Numbers of length 5 we need to multiply it with 100000 and take only whole number part

to get whole number part use Round Function with Decimal part =0

SQL

select round(rand() * 100000,0)

Now if we need 100 random Numbers Create a Procedure, Here is the one i am using with

–<START>
Alter proc generateRandomNumbers(@count as int)
as
declare @i as int=0
while @i<=@count
begin
print round(rand() * 100000,0)
–change this Statement to Insert to any table as you need
select @i=@i+1
end
–</END>

executing

exec generateRandomNumbers 10

Result

8544
19019
76087
72885
91053
13343
27529
50143
16858
87654
49888

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here