Use of random function

vishalneeraj-24503
Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 261
In Sql Server,Rand() function is used to generate any Random Values.

Note:- The output of the RAND function will always be a value between 0 and 1.

With Decimal values:
select rand() as random_number;

Output:
0.54376890154289


Without Decimal values:
select cast(rand()*1000000 as int) as random_number;

Output:
854980


Random top 10 rows from Table:
select top 10 rand() as random_number,emp_id,employee_name,email_id,address,pan_number
from employee_master;


Random top 10 rows with order by clause from Table:
select top 10 rand() as random_number,emp_id,employee_name,email_id,address,pan_number
from employee_master
order by rand();

Comments or Responses

Login to post response